GNU bug report logs -
#23832
sed combine d with q
Previous Next
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.
To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 23832 in the body.
You can then email your comments to 23832 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-sed <at> gnu.org
:
bug#23832
; Package
sed
.
(Thu, 23 Jun 2016 09:01:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Xen <xen <at> dds.nl>
:
New bug report received and forwarded. Copy sent to
bug-sed <at> gnu.org
.
(Thu, 23 Jun 2016 09:01:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
Hey, I am not sure if this is "by design" or not but....
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.
My idea was to use "/^$/{d;q}. I was under the assumption that both
commands would get executed.
However q is not executed.
However when we reverse it, and use sed "/^$/{p;q}"; the effect is that
the first matching newline (empty line) is printed twice, and then the
program quits. So {p;q} works, but {d;q} doesn't.
You are allowed to double the newline (empty line), but not remove it....
Now when I use:
sed -n "/^$/!p;/^$/q", as a way of not printing the first matching
newline, and then quitting, which is basically the same as deleting it
(this feels like if it is sunny weather; cry and moan, but you are not
allowed to do so; instead, you must cry when it is not cloudy, and moan
when it is sunny.
Same effect.
Just different logic ;-).
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". The
following will wipe everything, but it can't wipe that newline:
sed "/^\r$/{:again;N;s/.*\n.*//;t again}".
It's just funkily incredible :p.
Oh well. I had another few lifetimes to waste.... on something ;-).
Regards.
Information forwarded
to
bug-sed <at> gnu.org
:
bug#23832
; Package
sed
.
(Thu, 23 Jun 2016 14:19:02 GMT)
Full text and
rfc822 format available.
Message #8 received at 23832 <at> debbugs.gnu.org (full text, mbox):
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
Information forwarded
to
bug-sed <at> gnu.org
:
bug#23832
; Package
sed
.
(Thu, 23 Jun 2016 14:52:02 GMT)
Full text and
rfc822 format available.
Message #11 received at 23832 <at> debbugs.gnu.org (full text, mbox):
Assaf Gordon schreef op 23-06-2016 16:17:
> 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
> The "Q" command (a GNU Sed extension) might be of help:
That is so wonderful. I had yet not been imaginative enough to try the
uppercase version of that command. I was using an online tutorial that
has been there for a long time, but that never mentioned any such thing.
So thank you. Very much.
> The Q command quits without printing the pattern space.
So simple and so obvious :). (I mean the need for it, also). It appears
I was not the only person to think of this ;-).
Always happy to know you are not perfectly insane, only readily so ;-)
:p.
> To learn more about GNU sed extension command, see here:
> https://www.gnu.org/software/sed/manual/sed.html#Extended-Commands
Thanks.
> 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
Yes, I had not considered (or fully realized) that $ matches the last
line. I was trying to avoid having to use tail just for this single
thing.
> As such I'm closing this bug, but discussion can continue by replying
> to this thread.
You're welcome, and thanks.
Regards,
Bart.
Information forwarded
to
bug-sed <at> gnu.org
:
bug#23832
; Package
sed
.
(Thu, 23 Jun 2016 15:12:01 GMT)
Full text and
rfc822 format available.
Message #14 received at submit <at> debbugs.gnu.org (full text, mbox):
On Thu, 23 Jun 2016 10:31:58 +0200 (CEST), Xen <xen <at> dds.nl> wrote:
> Hey, I am not sure if this is "by design" or not but....
>
> 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.
>
> My idea was to use "/^$/{d;q}. I was under the assumption that both
> commands would get executed.
>
> However q is not executed.
>
> However when we reverse it, and use sed "/^$/{p;q}"; the effect is that
> the first matching newline (empty line) is printed twice, and then the
> program quits. So {p;q} works, but {d;q} doesn't.
>
> You are allowed to double the newline (empty line), but not remove it....
>
> Now when I use:
>
> sed -n "/^$/!p;/^$/q", as a way of not printing the first matching
> newline, and then quitting, which is basically the same as deleting it
> (this feels like if it is sunny weather; cry and moan, but you are not
> allowed to do so; instead, you must cry when it is not cloudy, and moan
> when it is sunny.
You can use
sed -n '/^$/q; p'
--
D.
Added tag(s) notabug.
Request was from
Assaf Gordon <assafgordon <at> gmail.com>
to
control <at> debbugs.gnu.org
.
(Tue, 24 Jan 2017 23:33:02 GMT)
Full text and
rfc822 format available.
bug closed, send any further explanations to
23832 <at> debbugs.gnu.org and Xen <xen <at> dds.nl>
Request was from
Assaf Gordon <assafgordon <at> gmail.com>
to
control <at> debbugs.gnu.org
.
(Tue, 24 Jan 2017 23:33:02 GMT)
Full text and
rfc822 format available.
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Wed, 22 Feb 2017 12:24:05 GMT)
Full text and
rfc822 format available.
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.