GNU bug report logs - #23832
sed combine d with q

Previous Next

Package: sed;

Reported by: Xen <xen <at> dds.nl>

Date: Thu, 23 Jun 2016 09:01:02 UTC

Severity: normal

Tags: notabug

Done: Assaf Gordon <assafgordon <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Assaf Gordon <assafgordon <at> gmail.com>
To: Xen <xen <at> dds.nl>, 23832 <at> debbugs.gnu.org
Subject: bug#23832: sed combine d with q
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





This bug report was last modified 8 years and 120 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.