Subject: bug#23832: sed combine d with q

From: Assaf Gordon <[email-masked]>
Date: Thu, 23 Jun 2016 10:17:54 -0400

tag 23832 notabug
close 23832
stop

Hello,

On 06/23/2016 04:31 AM, Xen wrote:
> Hey, I am not sure if this is "by design" or not but....
[...]
> I guess it is intentional. The d command is the only thing that can wipe a line, but it will stop command execution and "start a new cycle".

This behavior is by design, and mandated by POSIX:
"d - Delete the pattern space and start the next cycle."
http://pubs.opengroup.org/onlinepubs/009604599/utilities/sed.html#tag_04_126_13_03

> Suppose a text file with empty lines here and there. You want to print up to, but not including, the first newline.
>The first "^$", I mean.

The "Q" command (a GNU Sed extension) might be of help:

   $ printf "a\nb\n\nc\nd\n"
   a
   b
   
   c
   d

   $ printf "a\nb\n\nc\nd\n" | sed '/^$/Q'
   a
   b

The Q command quits without printing the pattern space.
To learn more about GNU sed extension command, see here:
   https://www.gnu.org/software/sed/manual/sed.html#Extended-Commands

Alternatively, If you can not use GNU extension, combining two 'sed' might be the simplest work-around:

   $ printf "a\nb\n\nc\nd\n" | sed '/^$/q' | sed '$d'
   a
   b

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

regards,
  - assaf