458

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?

PersianGulf
  • 10,850
roo
  • 4,731

2 Answers2

814

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.

peterh
  • 9,731
  • 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 Oct 06 '14 at 00:41
  • My exact usage is: sed -i 's/@r//g' hello.txt

    as I am trying to remove all occurances of '@r' from the file.

    – roo Oct 06 '14 at 00:45
  • 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 Jun 15 '15 at 21:45
  • Hmm.. In Yocto Poky I get error sed: can't move 'aJkTQCm' to 'folder': Is a directory (where aJkTQCm isn't a file or in any of their contents). In Debian Wheezy I get sed: couldn't edit /xxx: not a regular file. – geotheory Nov 20 '15 at 12:03
  • 18
    @roo on mac os x, using -ie I get that file foo is backup-ed as fooe. – jfbu Feb 25 '16 at 16:49
  • "s is used to replace the found expression...and g is used to replace any found matches."

    Don't these two phrases mean the same thing? "replace the found expression" / "replace any found matches"? I'm trying to make sense of these commands and what they do.

    – Jared May 11 '16 at 20:40
  • Found an explanation and corrected the solution: http://en.flossmanuals.net/command-line/ch043_sed/ – Jared May 11 '16 at 20:45
  • Is there not a more easy thing to use than SED for osx/unix? – SuperUberDuper May 14 '16 at 18:01
  • 5
    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
  • If you want to find and then replace this command may help you find . -type f -exec grep -i 'java' {} + -exec sed -i -e 's/java/java-1.7/g' {} + – Vishrant Jul 06 '16 at 15:22
  • It should be -i -e not -ie. If you use -ie you create a backup of every file with the letter 'e' appended to the filename. – Dave Hilditch Apr 25 '17 at 10:36
  • 11
    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 01 '17 at 19:13
  • @SuperUberDuper specify a zero-length extension to prevent backup on OSX: http://stackoverflow.com/questions/25486667/sed-without-backup-file – ericsoco May 17 '17 at 19:53
  • use double quote to expand using variables - sed -i "s/$foo/$bar/" hello.txt – Downhillski May 19 '17 at 13:19
  • -i option + "_bck" create a file backup: sed -i_bck -e 's/few/asd/g' hello.txt" – Alberto Oct 06 '17 at 08:28
  • This answer has too much text. Here's another which has detailed explanation without much text: https://askubuntu.com/a/20416/273214 – unknownerror Jun 08 '18 at 06:02
  • You should mention that the first character right after the s can be any character, and sed makes it the delimiting character. So, to replace paths in a file, use | for instance instead of / so that you can do the replacement. Ex: sed -i "s|/my/path/to/replace|/my/new/path|g" my_file.txt. – Gabriel Staples Apr 15 '20 at 02:37
91
sed -i 's/fea/asd/g'  hello.txt

g: Global

s: substitute

-i : realtime works with file inplace

alanjds
  • 103
PersianGulf
  • 10,850
  • 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 Jun 21 '19 at 09:16