Subject: bug#35195: sed '/^$/d' does not delete empty lines

From: Assaf Gordon <[email-masked]>
Date: Mon, 8 Apr 2019 16:07:53 -0600

tags 35195 notabug
close 35195
stop

Hello,

On 2019-04-08 6:02 a.m., Chin Huat Tan wrote:
> sed '/^$/d' does not delete empty lines on the attached file.

The attached file contains windows-style line-ends (\r\n , aka 
carriage-return + line feed):

    $ head -n 3 LLK-01.txt | od -c -An
     h   t   t   p   s   :   /   /   w   w   w   .   y   o   u   t
     u   b   e   .   c   o   m   /   w   a   t   c   h   ?   v   =
     t   w   x   q   l   U   K   d   Y   q   g  \r  \n  \r  \n   h
     t   t   p   s   :   /   /   w   w   w   .   y   o   u   t   u
     b   e   .   c   o   m   /   w   a   t   c   h   ?   v   =   L
     e   I   h   O   u   T   N   v   M   8  \r  \n

Therefor lines are not really empty (from unix POV),
as they contain a '\r' character.

You can use "dos2unix" utility to convert the file to unix-style.
     dos2unix LLK-01.txt
     sed '/^$/d' LLK-01.txt

You can use 'tr' to delete the '\r' characters:
     cat LLK-01.txt | tr -d '\r' | sed '/^$/d'

Or you can instruct sed to accept these characters as well:
     sed '/^\r*$/d' LLK-01.txt

As such I'm closing this as "not a bug", but discussion can continue by
replying to this thread.

regards,
  - assaf