Introduction
The sed (stream editor) utility is a line-oriented text parsing and transformation tool. The sed
command uses a simple programming language and regular expressions to process text streams and files. The most used feature of the sed
command is string substitution.
This guide shows how to use sed
to find and replace strings through examples.
Prerequisites
- Access to the command line/terminal.
- A text file (this guide provides an example.txt file).
- Basic terminal commands (grab our free cheat sheet).
sed Find and Replace Syntax
The syntax to find and replace text using the sed
command is:
sed -i 's/<search regex>/<replacement>/g' <input file>
The command consists of the following:
-i
tells thesed
command to write the results to a file instead of standard output.s
indicates the substitute command./
is the most common delimiter character. The command also accepts other characters as delimiters, which is useful when the string contains forward slashes.<search regex>
is the string or regular expression search parameter.<replacement>
is the replacement text.g
is the global replacement flag, which replaces all occurrences of a string instead of just the first.<input file>
is the file where the search and replace happens.
The single quotes help avoid meta-character expansion in the shell.
The BDS version of sed
(which includes macOS) does not support case-insensitive matching or file replacement. The command for file replacement looks like this:
sed 's/<search regex>/<replacement>/g' <input file> > <output file>
Alternatively, install the GNU version of sed
on macOS with homebrew:
brew install gnu-sed
Run the GNU sed
command as follows:
gsed -i 's/<search regex>/<replacement>/g' <input file>
Replace the sed
command with gsed
to follow the examples below.
sed Replace Examples
The examples from this guide use a sample file to replace strings.
1. Create a sample text file:
nano example.txt
2. Add the following contents:
foobarbazfoobarbaz
foo bar baz foo bar baz
Foo Bar Baz Foo Bar Baz
FOO BAR BAZ FOO BAR BAZ
/foo/bar/baz /foo/bar/baz
Use the file as input to test the examples below.
Note: For a full sed
command tutorial for Linux, check out our sed command Linux guide.
Replace First Matched String
1. To replace the first found instance of the word bar with linux in every line of a file, run:
sed -i 's/bar/linux/' example.txt
2. The -i
tag inserts the changes to the example.txt file. Check the file contents with the cat command:
cat example.txt
The command replaces the first instance of bar with linux in every line, including substrings. The match is exact, ignoring capitalization variations.
Global Replace
To replace every string match in a file, add the g
flag to the script. For example:
sed -i 's/bar/linux/g' example.txt
The command globally replaces every instance of bar with linux in the file.
Match and Replace All Cases
To find and replace all instances of a word and ignore capitalization, use the I
parameter:
sed -i 's/bar/linux/gI' example.txt
The command replaces all instances of the word bar in the text, ignoring capitalization.
Ignore Substrings
Add word boundaries (\b
) to the sed
command to ignore substrings when replacing strings in a file. For example:
sed -i 's/\bbar\b/linux/gI' example.txt
Alternatively, change the delimiter to make the command easier to read:
sed -i 's:\bbar\b:linux:gI' example.txt
The command ignores substrings, matching only the whole word.
Find and Replace Strings With Slashes
Escape the forward slash character to find and replace a string with slashes. For example, to replace /foo/bar/baz with /home/kb, use the following syntax:
sed -i 's/\/foo\/bar\/baz/\/home\/kb/g' example.txt
Alternatively, change the delimiter to avoid escaping characters:
sed -i 's:/foo/bar/baz:/home/kb:g' example.txt
Use this syntax to replace paths and other strings with slashes.
Find and Replace with Regular Expressions
The search pattern for the sed
command accepts regular expressions, similar to grep regex. For example, to match all capital letters and replace them with 5, use:
sed -i 's/[A-Z]/5/g' example.txt
The regex pattern helps find all capital letters and replaces them with the number in the file.
Reference Found String
Use the ampersand character (&
) to reference the found string. For example, to add a forward slash (/) before every instance of foo in a file, use:
sed -i 's/foo/\/&/gI'example.txt
Instead of retyping the search parameter, the &
sign references the found string.
Create a Backup
To create a backup file before overwriting the existing one, add the .bak
parameter to the -i
tag.
sed -i.bak 's/foo/FOO/g' example.txt
The command creates a backup (example.txt.bak
) before overwriting the original. Use this method to keep a copy in the original format and avoid overwriting.
Recursive Find and Replace
Use the find command to search for files and combine it with sed
to replace strings in files recursively. For example:
find -name 'example*' -exec sed -i 's/[a-z]/5/gI' {} +
The command finds all files starting with example and executes the sed
command on the files. The executed command replaces all letters with 5, ignoring capitalization.
Conclusion
After going through the examples in this guide, you know how to use sed
to replace strings in files. The sed
command is a powerful text manipulation utility with many advanced features.
Next, check out the awk or gawk command to learn about other text manipulation tools.