Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can't find any documentation about the sed -e switch, for simple replace, do I need it?


sed 's/foo/bar/'


sed -e 's/foo/bar/'
share|improve this question

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

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

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

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
share|improve this answer
+1 good tip for those commands. – glenn jackman Mar 1 '12 at 14:41
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

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.

share|improve this answer
-e script

Add the commands in script to the set of commands to be run while processing the input.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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