sed one Liner inverse range match-help required

cat test.txt


a



c
d

e


g




sed '/./,/^$/!d' test.txt 
a

c
d

e

g

Can some body explain how this inverse range match works for this example. i know sed command

/./,/^$/

means match the first line that contains atleast one character to the first blank line..but getting inverse of it is confusing

Read it like this: From the first non-empty line till the next blank line, DO NOT delete (explicit meaning); for the rest, DELETE (implicit).

1 Like

Can you please explain with contents in test.txt

in the input file
__________________
before 'a' there are 3 blank lines.
before 'c' there are 3 blank lines.
before 'e' there is 1 blank line.
before 'g' there are 2 blank lines.
after 'g' there are 4 blank lines.

output of sed command
__________________________-
before 'a' there is no blank line.
before 'c' there is 1 blank line.
before 'e' there is 1 blank line.
before 'g' there is 1 blank line.
after 'g' there is 1 blank line.

---------- Post updated at 05:55 PM ---------- Previous update was at 05:49 PM ----------

Ok man...I understood ..Thank you so much...