Issue
I found this question and answer on how to remove triple empty lines. However, I need the same only for double empty lines. Ie. all double blank lines should be deleted completely, but single blank lines should be kept.
I know a bit of sed, but the proposed command for removing triple blank lines is over my head:
sed '1N;N;/^\n\n$/d;P;D'
Solution
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
Answered By - Birei Answer Checked By - Senaida (WPSolving Volunteer)