Sed Examples: Search and Replace on Linux

Last updated:
Table of Contents

Usage of sed is closely linked to that of find. I'll try to keep examples relevant to sed only.

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

Search/replace string in file

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

Search/replace regular expressions in file

Note that sed uses a set of regexes different from grep and Perl1

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

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

For example, replace all digits with dashes in the target file:

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

Search/replace regular expressions 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

Search/Replace newlines in file

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

There is a workaround to allow you to replace redefine what sed considers a line.

To replace newlines (\n) with whitespace: (to modify the file in place, use sed -i instead)

# To modify the file in place, use `sed -i` instead
$ sed ':a;N;$!ba;s/\n/ /g' my_file.txt

Use with find: search/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'

HEADS-UP: "*.*" 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

1: These are Posix Basic Regexes. See more information here

References

Dialogue & Discussion