Using 'sed' to find and replace

  • 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?

    This should not be marked as a duplicate because the other answer referenced doesn't show you how to replace on a singular file, whereas this answer does. Either that or integrate this answer into that one

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

    My exact usage is: sed -i 's/@r//g' hello.txt as I am trying to remove all occurances of '@r' from the file.

    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.

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

    @roo on mac os x, using `-ie` I get that file `foo` is backup-ed as `fooe`.

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

    Found an explanation and corrected the solution:

    Is there not a more easy thing to use than SED for osx/unix?

    this command gives me an extra file ending with -e, why??: index.html-e **sed -i -e 's/n/d/g' index.html**

    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' {} +`

    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.

    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"

    @SuperUberDuper specify a zero-length extension to prevent backup on OSX:

    use double quote to expand using variables - `sed -i "s/$foo/$bar/" hello.txt`

    `-i option + "_bck"` create a file backup: `sed -i_bck -e 's/few/asd/g' hello.txt"`

    I wanna replace a URL .... how do i do that ? and it's in the middle of the file

    This answer has too much text. Here's another which has detailed explanation without much text:

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

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM