Introduction to Text Manipulation
sed (Stream Editor) is a powerful, non-interactive text editor that processes input line by line, performing transformations (like searching and replacing).
The Substitution Command (s)
The most common use of sed is substitution, using the format: s/pattern/replacement/flags.
Basic Substitution (First Occurrence)
Replace the first occurrence of 'old' with 'new' on each line:
bash $ echo 'old file old config' | sed 's/old/new/' new file old config
Global Substitution (g flag)
To replace all occurrences on a line, use the g (global) flag.
bash $ echo 'old file old config' | sed 's/old/new/g' new file new config
In-Place Editing (-i)
By default, sed prints the result to STDOUT. The -i option modifies the file directly (in-place).
bash
Permanently change 'localhost' to '127.0.0.1' in my_config.txt
$ sed -i 's/localhost/127.0.0.1/g' my_config.txt