292

This question already has an answer here:

I know this question has probably been answered before. I have seen many threads about this in various places, but the answers are usually hard to extract for me. I am looking for help with an example usage of the 'sed' command.

Say I wanted to act upon the file "hello.txt" (in same directory as prompt). Anywhere it contained the phrase "few", it should be changed to "asd". What would the command look like?

marked as duplicate by Braiam, peterph, slm Oct 7 '14 at 1:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

565

sed is the stream editor, in that you can use | (pipe) to send standard streams (STDIN and STDOUT specifically) through sed and alter them programmatically on the fly, making it a handy tool in the Unix philosophy tradition; but can edit files directly, too, using the -i parameter mentioned below.
Consider the following:

sed -i -e 's/few/asd/g' hello.txt

s/ is used to substitute the found expression few with asd:

The few, the brave.


The asd, the brave.

/g stands for "global", meaning to do this for the whole line. If you leave off the /g (with s/few/asd/, there always needs to be three slashes no matter what) and few appears twice on the same line, only the first few is changed to asd:

The few men, the few women, the brave.


The asd men, the few women, the brave.

This is useful in some circumstances, like altering special characters at the beginnings of lines (for instance, replacing the greater-than symbols some people use to quote previous material in email threads with a horizontal tab while leaving a quoted algebraic inequality later in the line untouched), but in your example where you specify that anywhere few occurs it should be replaced, make sure you have that /g.

The following two options (flags) are combined into one, -ie:

-i option is used to edit in place on the file hello.txt.

-e option indicates the expression/command to run, in this case s/.

Note: It's important that you use -i -e to search/replace. If you do -ie, you create a backup of every file with the letter 'e' appended.

  • I'm using OS X, for what that's worth, but this is the error I get: sed: 1: "hello.txt": invalid command code . – roo Oct 6 '14 at 0:41
  • 7
    An edit was proposed to add the -e flag to sed. The -e flag is optional as long as the command to execute appears first, there is not a command file and a command arg, and there is only one command. – hildred Jun 15 '15 at 21:45
  • 11
    @roo on mac os x, using -ie I get that file foo is backup-ed as fooe. – jfbu Feb 25 '16 at 16:49
  • 3
    this command gives me an extra file ending with -e, why??: index.html-e sed -i -e 's/n/d/g' index.html – SuperUberDuper May 14 '16 at 18:19
  • 6
    On Mac, -i -e creates a new file with -e appended to the name. This seems to work in bash: sed "-i" "" "-e" "s/few/asd/g" "somefile.xml" – Olivier Lalonde May 1 '17 at 19:13
62
sed -i 's/fea/asd/g'  hello.txt

g: Global

s: substitute

-i : realtime works with file inplace

  • 1
    When dealing with symlinks, it is good to add --follow-symlinks otherwise -i will turn symlink into a (edited) copy of the file. – user7610 Jun 21 at 9:16

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