41

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/'

migrated from stackoverflow.com Mar 1 '12 at 17:30

This question came from our site for professional and enthusiast programmers.

  • 14
    Are you sure you didn't find this in man sed? – BoltClock Mar 1 '12 at 8:32
  • 4
    You don't find potongs explanation, why not to use "cmd1;cmd2" – user unknown Mar 1 '12 at 22:51
28

From 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.

  • 3
    So, you don't need '-e' if you only have one script, right? – JohnyTex Aug 8 '17 at 10:26
  • I, too, am searching for the true purpose of -e – typelogic Oct 22 '18 at 17:14
27

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
  • 1
    On at least some sed implementations (for instance, on FreeBSD), you also need to end labels and branch commands using newlines/-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" – dubiousjim Oct 16 '12 at 7:35
  • I'm not sure how necessary is the 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 ? – typelogic Oct 22 '18 at 17:11
1

-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.

0

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

But 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.