bug-sed
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#23832: sed combine d with q


From: Assaf Gordon
Subject: bug#23832: sed combine d with q
Date: Thu, 23 Jun 2016 10:17:54 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0

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






reply via email to

[Prev in Thread] Current Thread [Next in Thread]