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'hellohello2
Why can't I sed two [or more..] empty lines to one empty line?
sed
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 linesIt works like this:$ echo -e hello\n\n\nworld | cat -shelloworldIf you want to remove the blank lines entirely rather than compressing them, use grep:$ echo -e hello\n\n\nworld | grep -v '^$'helloworldNote 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'helloworldYou can see why cat -s is a good deal easier if collapsing multiple blank lines is all you are after!