Finding and Replacing Text with sed
Finding and replacing text is the job sed is best known for. Its substitute command, s, searches each line for a regular expression and replaces the matched text with new text. Scripts use it to update configuration values, rename paths, clean up command output, reformat dates, hide sensitive values in logs and much more, all without opening an editor.
Lesson 106 introduced sed alongside grep and awk. This lesson covers the s command in depth: its flags, regular expressions, captured groups, addresses that limit which lines change, editing files in place, and safely using shell variables in a substitution.
The Substitute Command
The basic form is:
sed 's/PATTERN/REPLACEMENT/FLAGS' file
| Part | Meaning |
|---|---|
s | The substitute command |
/ | The delimiter that separates the parts (any character can be used, see below) |
PATTERN | A regular expression to search for |
REPLACEMENT | The text that replaces the match |
FLAGS | Optional letters or numbers that change how the substitution works |
sed reads one line at a time into an internal buffer called the pattern space, applies the command, prints the result, and moves to the next line. The input file is not changed unless -i is used.
Replacing the First Match or Every Match
Without flags, s replaces only the first match on each line:
echo 'cat cat cat' | sed 's/cat/dog/'
dog cat cat
The g (global) flag replaces every match on the line:
echo 'cat cat cat' | sed 's/cat/dog/g'
dog dog dog
A number replaces only that occurrence, and in GNU sed a number combined with g replaces that occurrence and all later ones:
echo 'cat cat cat' | sed 's/cat/dog/2'
echo 'cat cat cat' | sed 's/cat/dog/2g'
cat dog cat
cat dog dog
GNU sed also accepts I to ignore case:
echo 'Cat CAT cat' | sed 's/cat/dog/Ig'
dog dog dog
I is a GNU extension. BSD sed on macOS does not support it.
The Pattern Is a Regular Expression
The pattern is not plain text. Characters such as ., *, [, ^ and $ have special meanings. A dot matches any single character:
echo 'v1.2 v1x2' | sed 's/1.2/ONE/g'
echo 'v1.2 v1x2' | sed 's/1\.2/ONE/g'
vONE vONE
vONE v1x2
The first command also changed 1x2, because . matched the x. A backslash before the dot makes it a literal dot.
^ anchors the match to the start of the line and $ to the end. Anchoring prevents accidental partial matches:
printf 'port=80\nexport=1\n' | sed 's/^port=/PORT=/'
PORT=80
export=1
Without ^, a pattern port= would also match inside export=.
Basic and Extended Regular Expressions
By default sed uses basic regular expressions (BRE). In BRE, +, ?, |, (, ), { and } are ordinary characters, and their special meanings need a backslash. The -E option switches to extended regular expressions (ERE), where they are special without a backslash:
echo 'aaa b' | sed 's/a+/X/' # BRE: + is a literal plus sign
echo 'aaa b' | sed 's/a\+/X/' # GNU BRE: \+ means one or more
echo 'aaa b' | sed -E 's/a+/X/' # ERE: + means one or more
aaa b
X b
X b
The first command found no literal + and changed nothing.