Sed Examples: Search and Replace on Linux

Sed Examples: Search and Replace on Linux

Last updated:
Table of Contents

Usage of sed is closely linked to that of find. Examples here focus on sed only.

To see different ways to use find, look at Linux find examples

Replace string

To modify the file in place, use sed -i instead

Replace all occurrences of foo with bar in my_file.txt.

$ sed 's/foo/bar/' my_file.txt

Replace regex

sed uses extended posix regular expressions. Read more about those here

To modify the file in place, use sed -i -r in this case

The -r flag enables the use of extended posix regular expressions.

Example: replace all digits with dashes in the target file:

$ sed -r 's/[0-9]/-/g' my_file.txt

Replace regex with match groups

To modify the file in place, use sed -i -r instead

In order to use the results of a match in the "search" part in the "replace" part of the sed command, use "\"+match_number. For example, to add a 'X' to the end of all numbers in a file:

$ sed -r 's/([0-9]+)/\1X/g' my_file.txt

In this example, I've used \1 to match the first match group (match groups are stuff that's between parentheses). If there were more, you could also use \2,\3 and so on to represent the next matches.

Replace whole lines matching pattern

To modify the file in place, use sed -i instead

For example, replace the line that matches "FOO BAR" anywhere in it with "The quick brown fox jumps over the lazy dog":

$ sed '/FOO BAR/c\The quick brown fox jumps over the lazy dog' my_file.txt

So if you had something like this:

Lorem ipsum dolor sit amet
consectetur FOO BAR adipiscing elit
Nullam a dui sapien.

It becomes this:

Lorem ipsum dolor sit amet
The quick brown fox jumps over the lazy dog
Nullam a dui sapien.

Delete lines matching pattern

To modify the file in place, use sed -i instead

Remove all lines that match the given pattern.

$ sed '/foo bar/d' my_file.txt

Replace newlines

sed is line-based, so it's hard for it to work with newlines. Use tr instead

Although sed is line-based (so it doesn't see across lines) there is a workaround to allow you to redefine what it considers a line.

Example: Replace newlines (\n) with whitespace:

$ sed ':a;N;$!ba;s/\n/ /g' my_file.txt > output_file.txt

Replace one or more newlines

It's a variation of the above.

Example: Replace one or more newlines \n in file input.txt with whitespace

$ (sed -r ':a;N;$!ba;s/\n+/ /g' | sed -r 's/\s+/ /g') < input.txt > out.txt

  • input.txt:

    foo
    
    bar     
    
    baz
    quux
    
  • out.txt:

    foo bar baz quux
    

Use with find: replace in current directory and under

For example, to replace all occurrences of "foo" with "bar" in all files in the current directory and under:

Note that the files are modified in place

$ find . -name "*.*" -type f | xargs sed -i 's/foo/bar/g'

Caution! "*.*" means all files! This also includes files like those under Code Versioning Tools, like SVN or GIT. Running this command on a directory that includes these files may break your repositories! use with caution!

Escape single quotes

Use double quotes as delimiters:

To modify the file in place, use sed -i instead

$ sed "s/''/'foobar'/g" my_file.txt

References