In this tutorial, we’ll explore one of sed’s most powerful features: find-and-replace. It assumes a basic familiarity with sed.
Simple find and replace
Example
echo -e 'find\nfind\nfind' | sed 's/find/replace/'
Output
replace replace replace
Explanation
Let’s break it down:
s is the command.
/find/replace/ is the option. It means any string "find" should be changed with "replace"
Simple find and replace only on the 2nd line
Example
echo -e 'find\nfind\nfind' | sed '2 s/find/replace/'
Output
find replace find
Explanation
The only difference in the command and the simple case is that there’s a "2" before the s. This sets the address of the command to just the 2nd line.
Global find and replace
Example
echo 'find find find' | sed 's/find/replace/g'
Output
replace replace replace
Explanation
The only difference in the command and the simple case is that there’s a "g" after the last slash. This makes the find and replace apply to all instances.
2nd match find and replace
Example
echo 'find find find' | sed 's/find/replace/2'
Output
find replace find
Explanation
The only difference in the command and the simple case is that there’s a "2" after the last slash. This makes the find-and-replace apply to the 2nd match If there is only one match, nothing is applied.
2nd match and beyond find and replace
Example
echo 'find find find' | sed 's/find/replace/2g'
Output
find replace replace
Explanation
The only difference in the command and the simple case is that there’s a "2g" after the last slash. This makes the find-and-replace apply to the 2nd match and beyond . If there is only one instance, nothing is applied.
find and replace a /
Example
echo -e '/path/to/file\npath' | sed 's%/path/%/new_path/%'
Output
/new_path/to/file path
Explanation
The slash is the canonical delimiter, but any character can be used. When dealing with text containing slash, % is a natural choice.
find and replace a / using escaping
Example
echo -e '/path/to/file\npath' | sed 's/\/path\//\/new_path\//'
Output
/new_path/to/file path
Explanation
Let’s break this down /\/path\//\/new_path\//
/
\/path\/
/
\/new_path\/
/
As we see, we can use \ to escape / to include a / when / is the delimiter.
Regular-expression matches
Example
echo -e 'find fend found' | sed 's/f.nd/replace/g'
Output
replace replace found
Explanation
sed can also use a regular expression to find matches. As f.nd matches find and fend, but not found, we can see why they are replaced but not found
Replacement special value: &
Example
echo -e 'a.txt\nb.txt' | sed 's/.*/cp & &.bak/g'
Output
cp a.txt a.txt.bak cp b.txt b.txt.bak
Explanation
& in the replacement text means the whole match. Here the match is file, so & is equivalent to a.txt in the first match, and b.txt in the second match. Here we use this sed command takes a list of files from stdin and outputs a program that backups the files. To run the program, pipe it to bash:
echo -e 'a.txt\nb.txt' | sed 's/.*/cp & &.bak/g' | bash
That’s it
Conclusion
We’ve just seen some basic features of sed’s find and replace. Next week, we’ll dig into some of the CLI flags, like the one that lets us use sed commands to modify a file. If you don’t want to miss it, just click the subscribe now button below
Very useful. Thanks