I have a file that contains:
something something else something else again
I need a bash command, sed/grep w.e that will produce the following output
something something else something else again
In other words, I need to replace multiple blank lines with just a single blank line. grep/sed are line based. I've never found a BASH solution that would work on multi-line regex patterns.
If you aren't firing vim or sed for some other use, cat actually has an easy builtin way to collapse multiple blank lines, just use cat -s .
For BSD-derived systems (including GNU):
You just need cat
with the -s
option which causes it to remove repeated empty lines from its output:
cat -s
From man page: -s --squeeze-blank: suppress repeated empty output lines.
I just solved this problem by sed
. Even if this is a 7 year old question, someone may find this helpful, so I am writing my solution by sed
here:
sed 'N;/^\n$/D;P;D;'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With