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?


2 Answers 2


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.

  • 2
    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
    Commented Oct 6, 2014 at 0:41
  • 10
    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
    Commented Jun 15, 2015 at 21:45
  • 20
    @roo on mac os x, using -ie I get that file foo is backup-ed as fooe.
    – jfbu
    Commented Feb 25, 2016 at 16:49
  • 6
    this command gives me an extra file ending with -e, why??: index.html-e sed -i -e 's/n/d/g' index.html Commented May 14, 2016 at 18:19
  • 12
    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" Commented May 1, 2017 at 19:13
sed -i 's/fea/asd/g'  hello.txt

g: Global

s: substitute

-i : realtime works with file inplace

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

Not the answer you're looking for? Browse other questions tagged .