2

Why does not this sed command remove empty lines?

sed -e 's/\r//g' -e '/^{$/d' -e '/^}$/d' -e ':begin;$!N;/^state-text: {[[:space:]]\n/s/\n//;tbegin;P;D' -e "s/'/''/g" -e '/^$/d' <<< $myvar

This command all works fine except the last part -e '/^$/d' for removing the empty lines which appears to be doing nothing at all. Here is a sample of my content:

begin dump

Ctrl_ID:100

This is the same content viewed with cat -A

begin dump$
$
Ctrl_ID:100$

Examining the empty line with an Hex editor shows that all it contains is one single char with the hex value

0A

Which is exactly the same code at the end of each lines.

I don't understand what's going on here.

^$ should match, no?

3

Try this (empty line remove is now at first position):

sed -e '/^$/d' -e 's/\r//g' -e '/^{$/d' -e '/^}$/d' -e ':begin;$!N;/^state-text: {[[:space:]]\n/s/\n//;tbegin;P;D' -e "s/'/''/g" <<< $myvar

The part, which is responsible for the not working empty-line-remove is this one:

-e ':begin;$!N;/^state-text: {[[:space:]]\n/s/\n//;tbegin;P;D'

going deeper...

D deletes the contents of the patterns space, up to the first newline (or to the end if there is no newline), and starts a new cycle. The latter means that any commands that come after the D in the sed program will not be executed if D itself is executed.

from here

  • That, actually works... why? oh WHY?!! my WTF moment has been upgraded to first class now lol. – TCZ8 Jun 13 '14 at 13:50
  • Figures... its the only part of the sed command I don't fully understand. Got that one off the web and adapted it for my script. – TCZ8 Jun 13 '14 at 14:01
  • Oh wow. This would actually have caused bugs in the future that would have been a head ache to fix. Thank you very much for your help and catching this. – TCZ8 Jun 13 '14 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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