Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto use sed to remove only triple empty lines?

Tags:

linux

sed

Howto use sed to remove only triple empty lines?

For example:

MyText.txt

line1

line2


line3



line4

with the use of sed i want the result to look like this
MyText.txt

line1

line2


line3
line4

I was able to delete double empty lines with

sed -i '/^$/{
N
/^\n$/D
}' MyText.txt

However my goal is to delete triple empty lines and only triple empty lines.

Any help would be much appreciated.

like image 829
Flan Alflani Avatar asked Jan 10 '11 21:01

Flan Alflani


2 Answers

It's as simple as:

sed '1N;N;/^\n\n$/d;P;D'
like image 85
Dennis Williamson Avatar answered Nov 03 '22 20:11

Dennis Williamson


It's not sed, but it's a whole lot shorter than what you can do with sed:

$ printf 'a\nb\n\nc\n\n\nd\n' | 
  perl -e 'undef $/; $_ = <>; s/\n\n\n/\n/g; print'
a
b

c
d
like image 23
zwol Avatar answered Nov 03 '22 22:11

zwol