[Top][All Lists]

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

bug#31781: I apologize it's not the right place, but I need answer

From: Eric Blake
Subject: bug#31781: I apologize it's not the right place, but I need answer
Date: Tue, 12 Jun 2018 06:27:31 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0

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 '
  ' <$as_myself |
    sed '
      t lineno
      t loop
    ' >$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'

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

reply via email to

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