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…