How to use sed to remove only double empty lines?

This would be easier with cat:

cat -s

I've commented the sed command you don't understand:

sed '
    ## In first line: append second line with a newline character between them.
    1N;
    ## Do the same with third line.
    N;
    ## When found three consecutive blank lines, delete them. 
    ## Here there are two newlines but you have to count one more deleted with last "D" command.
    /^\n\n$/d;
    ## The combo "P+D+N" simulates a FIFO, "P+D" prints and deletes from one side while "N" appends
    ## a line from the other side.
    P;
    D
'

Remove 1N because we need only two lines in the 'stack' and it's enought with the second N, and change /^\n\n$/d; to /^\n$/d; to delete all two consecutive blank lines.

A test:

Content of infile:

1


2
3

4



5

6


7

Run the sed command:

sed '
    N;
    /^\n$/d;
    P;
    D
' infile

That yields:

1
2
3

4

5

6
7

sed '/^$/{N;/^\n$/d;}'

It will delete only two consecutive blank lines in a file. You can use this expression only in file then only you can fully understand. When a blank line will come that it will enter into braces.

Normally sed will read one line. N will append the second line to pattern space. If that line is empty line. the both lines are separated by newline.

/^\n$/ this pattern will match that time only the d will work. Else d not work. d is used to delete the pattern space whole content then start the next cycle.


This would be easier with awk:

awk -v RS='\n\n\n' 1