Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Why can't I sed two [or more..] empty lines to one empty line? What is the trick?

echo -e "hello\n\n\nhello2" | sed 's/^$\n^$/\n/g'

share|improve this question
up vote 10 down vote accepted

The reason your sed failed is that unless you specify a multi-line operator, it operates on the stream one line at a time. Multiple beginning of line ^ and end of line $ operators are meaningless when strung together like that if you are only looking at the text one line at a time.

The easist way to collapse multiple blank lines is with cat. From the man page:

-s, --squeeze-blank
suppress repeated empty output lines

It works like this:

$ echo -e "hello\n\n\nworld" | cat -s


If you want to remove the blank lines entirely rather than compressing them, use grep:

$ echo -e "hello\n\n\nworld" | grep -v '^$'

Note that if you really want to do this in sed you have to use complicated expressions and actions. Here is an example (thanks to fred) that collapses any number of sequencial blanks into a single blank line:

$ echo -e "hello\n\n\nworld" | sed -re '$!N;/^\n$/!P;D'


You can see why cat -s is a good deal easier if collapsing multiple blank lines is all you are after!

share|improve this answer
you're a real living god. – LanceBaynes Aug 6 '11 at 12:20
@LanceBaynes: Nope, just a man who's had the same question and read enough other people's code to notice the answers. – Caleb Aug 6 '11 at 12:23
Here is O'Reilly's Command Summary for sed – Peter.O Aug 6 '11 at 14:22
@Calab: The sed example I posted earlier was for a general delete of any duplicated lines bar one (like uniq ).. It should be: sed -re '$!N;/^\n$/!P;D' to cater for just blank lines... my mistake, sorry... – Peter.O Aug 6 '11 at 17:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.