sed trick

This is about how to remove all blank lines after numbers, but not lines after letters
The challenge is that sed usually process line by line, and it does not correlate lines.
Therefore we have to put all lines into one pattern space.
$ sed -E ‘:a;N;$!ba;s/([[:digit:]])\n/\1/2g’ rmBlankLine.txt

-E: use regular expression
all commands in single quotation mark, and each command ends with ;
:a is label.
N means to append next line with newline
ba means go to label a
! means not to process last line
basically after “:a;N;$!ba;”, the whole file is in one pattern space.
([[:digit:]]) is the first capture group, and we output as is as seen at \1
and we dropped the \n after the first capture group
2g means to release after Line2 to end of file, therefore the newline after 2 is not removed


Previous Post
Next Post
Comments are closed.
%d bloggers like this: