GNU bug report logs -
#31781
I apologize it's not the right place, but I need answer
Previous Next
Reported by: Budi <budikusasi <at> gmail.com>
Date: Sun, 10 Jun 2018 22:59:01 UTC
Severity: normal
Tags: notabug
Done: Assaf Gordon <assafgordon <at> gmail.com>
Bug is archived. No further changes may be made.
Full log
Message #15 received at 31781-done <at> debbugs.gnu.org (full text, mbox):
On 06/12/2018 04:04 AM, Assaf Gordon wrote:
>> How to use SED command(s) that will replace the shell's pipe command in
>> order to perform more efficient, e.g:
>>
>> echo abcde | sed -r 's/cd/XX/' | sed 's/[^x]/z/ig'
Many times, it IS possible to use a single sed process instead of a
pipeline of two consecutive sed processes, or to turn 'grep ... | sed
...' into a single sed process. But this is not universally true -
there are some cases where pipelining two sed processes together is
required (where no single sed process will accomplish the same task).
So, there is no generic way to rewrite a sed pipeline into a single script.
For an example, typical configure scripts produced by Autoconf include
this pipeline, including the comment:
# Blame Lee E. McMahon (1931-1989) for sed's syntax. :-)
sed -n '
p
/[$]LINENO/=
' <$as_myself |
sed '
s/[$]LINENO.*/&-/
t lineno
b
:lineno
N
:loop
s/[$]LINENO\([^'$as_cr_alnum'_].*\n\)\(.*\)/\2\1\2/
t loop
s/-\n.*//
' >$as_me.lineno
There is NO WAY to rewrite that into a single sed process (the first
process prints every line and also appends a line number line any time
literal '$LINENO' was seen; the second process then folds the line
numbers in place of the '$LINENO' occurrences). But then again, that
second use of sed is much more involved than your typical one-liner
substitution.
>
> The portable way is to use multiple "-e" parameters:
>
> $ echo abcde | sed -e 's/cd/XX/' -e 's/[^x]/z/ig'
> zzXXzz
>
> That should work on all sed, not just gnu sed.
Yes, when combining two one-liner substitutions that both operate on
every line, that should work. But once the sed script gets more
involved, such as using 'sed -n' with 's///p' or using addresses to
limit which lines are acted on, you have to be sure that the second half
of your rewrite only performs in the same cases where it would get
output from the first half when it was two separate processes.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
This bug report was last modified 6 years and 339 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.