Given this:
echo AAA | sed -r 's/A/echo B/ge'
I get this:
Becho Becho B
I would have thought I would get "BBB". This is with GNU sed version 4.2.1. What is going on, and how can I use the execute flag, and have multiple replacements can occur on one line (from the shell, not from perl et al)?
Best Answer
The flags work together in the opposite way to what you're expecting. The documentation of
/e
is, for the record:That is a bit tortuously written. What it means is that, after the completion of a
s///
command for this line, if there was a change, the (new) line is executed as a command and its output used as the replacement for this line.So for your given command:
it first replaces each
A
withecho B
, and then executes the result as a command. It has (roughly speaking) the same effect as:GNU
sed
does not directly support the mode you want, although you can fake it with a more complex script if desired. Alternatively, Perl's/e
modifier to itss
command does have the behaviour you're looking for, but with Perl expressions instead.