I can't find any documentation about the sed
-e
switch, for simple replace, do I need it?
e.g.
sed 's/foo/bar/'
VS
sed -e 's/foo/bar/'
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityFrom the man page:
-e script, --expression=script
add the script to the commands to be executed
So you can use multiple -e
options to build up a script out of many parts.
$ sed -e "s/foo/bar/" -e "/FOO/d"
Would first replace foo
with bar
and then delete every line containing FOO
.
This might work for you:
sed -e '/foo/i\' -e 'bar' -e '/fred/a\' -e 'barny' -e '/harry/c\' -e 'potter' file
In each case the i
(insert),a
(append) and c
(change) commands need to be terminated by a newline.
Normally commands can be separated by a ;
e.g. /foo/d;/bar/d
and grouped by {...
} e.g. /foo/{h;d}
but for the i,a,c
commands the -e
provides a way of separating the commands.
The alternative is to use the shell(bash) to insert a newline:
sed '/foo/i\bar'$'\n''/fred/a\barney'$'\n''/harry/c\potter' file
-e
, rather than with semicolons. This won't work: sed ':a; /x/ { s/x/y/g; ba }; q' <<< "jxm"
. But this will: sed -e ':a' -e '/x/ { s/x/y/g; ba' -e '}; q' <<< "jxm"
Oct 16, 2012 at 7:35
must be terminated with a newline
phrase means here. When I tested here in my cygwin
, this works: sed '/foo/i\bar' file
. So now that we can fuse together the separate expressions into one, this raises again to the original question: What is the true purpose of the -e
?
In the help for sed
you will find that -e
flag stands for:
-e script, --expression=script
add the script to the commands to be executed
You are not the first to be confused after encountering -e
. In your example sed 's/foo/bar/'
and sed -e 's/foo/bar/'
are equivalent. In both cases s/foo/bar/
is the script that is executed by sed. The second option is more explicit, but that is probably not the reason that you often see -e
used. The reason for that is that -e
makes it possible to use more than one script with the same invocation of sed
. That still leaves the question why use it if you are using only one script? The following example might explain that:
echo "foo" | sed -e 's/foo/bar/' -e 's/bar/foo/'
Will output "foo".
However,
echo "foo" | sed 's/foo/bar/' -e 's/bar/foo/'
will fail because now sed
interprets the first s/foo/bar/
as a filename. Hence, to make life easier for future you, you can already start using the -e
flag today. That way future you can just append the command with an extra script by using -e <script>
.
N.B. In other answers using ;
as an alternative to use multiple scripts has been suggested. While that is technically possible in some cases, it is not encouraged, because it quickly leads to very hard to read code. See an example here: https://www.grymoire.com/Unix/Sed.html#uh-61
-e
, equivalent to --expression
, is optional, unless you are stringing several expressions together (not common) as shown in another Answer.
Aside: ;
can be used instead in the same expression to execute several expressions one after the other.
man sed
?