I'm using Ubuntu and have a large text file where certain rows start with <
character what I'd like to replace each of these to an empty row. So from this:
eeeeee
<
<
aaaa
bbbb
cccc
<
dddddd
<
ff
I want this:
eeee
aaaa
bbbb
cccc
dddddd
ff
(In case of multiple consecutive <
rows only one empty rows is needed ideally)
How to perform this in command line?
CodePudding user response:
You could use sed. In this case the command would look something like:
sed '/</c\\' file.txt
This would find a line with a '<' character at the beginning, and replace the line with nothing. It would not however replace multiple empty rows with a single row.
CodePudding user response:
This Perl one-liner should do what you're asking for:
perl -ne 'if (/^</) {print "\n" if !$f; $f=1} else {$f=0; print}' tmp.txt
Here it is in action:
# cat tmp.txt
eeeeee
<
<
aaaa
bbbb
cccc
<
dddddd
<
ff
# perl -ne 'if (/^</) {print "\n" if !$f; $f=1} else {$f=0; print}' tmp.txt
eeeeee
aaaa
bbbb
cccc
dddddd
ff
Commented code:
# Found a line starting with '<'
if (/^</) {
# Print a blank line if $f is false
print "\n" if !$f;
# Set $f to true so subsequent lines starting with '<' are ignored
$f=1;
} else {
# Not a line starting with '<'; reset $f to false
$f=0;
# Print the current line
print;
}