'How to replace rows starting with a certain character with emptly lines in text file with Linux/Unix?
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?
Solution 1:[1]
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;
}
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | plentyofcoffee |
| Solution 2 | jjbmiller |
