I have written a sed script, which replaces multiple blank lines with a single one, but it is not working as supposed to. I will be thankful to everybody, who can explain me why. Please do not refer me to working examples, I am familiar with Google. I just want to understand how sed works.
The code is
sed ':a;/^\n*$/{N;ba};s/^\n\n*/\n/' input_file
so the logic is simple: when sed reads the line and it is either blank or has several newline symbols (this is /^*\n$
condition), I tell sed to append the next line to pattern space. as soon as a non-blank line is found, the substitution s/^\n\n*/\n/
is done.
Everything works fine except the cases when I have blank lines in the end of the file. These blanks are not replaced with a single blank and I do not understand why.
Any ideas?