I can't understand how this sed script works:
echo -e "Line #1\n\n\n\nLine #2\n\n\nLine #3" | sed '1s/^$//p;/./,/^$/!d' It suppresses repeated empty lines like cat -s But I have a couple of questions:
- For what
1s/^$//p? As I understand it do nothing with the first line even if it empty - Is this
/./,/^$/matches only before first^$likeLine #1\n\nand not matchesLine #1\n\n\n? - Are ranges not greedy by default in sed?
To clarify question 3 I tried next tests:
echo -e "Line #1\n\n\n\nLine #2\n\n\nLine #3" | sed -n '/#/,/#/p'
And result was:
Line #1 Line #2 Line #3 (so, it is greedy)
But when I tried:
echo -e "Line #1\n\n\n\nLine #2\n\n\nLine #3" | sed -n '/#1/,/#/p' result was:
Line #1 Line #2 (now it seems to be not greedy)
1{/^$/p}, but some other seds (like the one on my Mac) may be more picky about requiring newlines with the braces.