Bash Sed
The sed command edits text as it flows through a pipe or file. The name stands for stream editor. Sed replaces words, deletes lines, and inserts text without opening an editor window. It works like the "Find and Replace" tool, controlled by a single command.
How Sed Works
+-----------+ +----------------------+ +------------+
| Input | --> | sed reads one line | --> | Edited |
| text | | applies the rule | | output |
| | | repeats for others | | |
+-----------+ +----------------------+ +------------+
(the original file stays unchanged)
Sample File
Create notes.txt with these lines:
apple is red
banana is yellow
apple pie is sweet
grape is purpleReplace Text
The letter s stands for substitute. The syntax is s/old/new/.
sed 's/apple/mango/' notes.txtOutput:
mango is red
banana is yellow
mango pie is sweet
grape is purpleThe original file stays the same. Sed prints the edited version on the screen.
Replace Every Match on a Line
Sed changes only the first match per line by default. The g flag (global) changes all of them.
echo "one one one" | sed 's/one/two/'
echo "one one one" | sed 's/one/two/g'Output:
two one one
two two twoIgnore Case
sed 's/APPLE/mango/I' notes.txtThe I flag matches uppercase and lowercase letters alike.
Save Changes to the File
The -i option edits the file directly:
sed -i 's/apple/mango/g' notes.txtCreate a backup copy first by adding a suffix after the option:
sed -i.bak 's/apple/mango/g' notes.txtmacOS needs a slightly different form: sed -i '' 's/apple/mango/g' notes.txt.
Delete Lines
The letter d deletes lines.
sed '2d' notes.txt # delete line 2
sed '2,3d' notes.txt # delete lines 2 to 3
sed '/banana/d' notes.txt # delete lines that contain banana
sed '$d' notes.txt # delete the last line
sed '/^$/d' notes.txt # delete empty linesPrint Selected Lines
The -n option hides the automatic output. The letter p prints chosen lines.
sed -n '2p' notes.txt # print line 2
sed -n '1,2p' notes.txt # print lines 1 and 2
sed -n '/grape/p' notes.txt # print lines with grapeInsert and Append Lines
sed '1i Fruit List' notes.txt # insert before line 1
sed '$a End of list' notes.txt # append after the last lineUse a Different Separator
Folder paths contain slashes, which clash with the sed syntax. Pick another symbol such as | or #:
echo "/home/asha/docs" | sed 's|/home/asha|/home/ravi|'Output:
/home/ravi/docsCapture and Reuse Parts
Parentheses group text, and \1 brings the group back. The -E option removes the need for backslashes before parentheses.
echo "Asha Sharma" | sed -E 's/(\w+) (\w+)/\2, \1/'Output:
Sharma, AshaRun Several Edits at Once
sed -e 's/apple/mango/' -e 's/grape/lemon/' notes.txtPractical Example: Clean a Settings File
Remove comment lines and blank lines from a configuration file:
sed -e '/^#/d' -e '/^$/d' settings.confChange a setting value in place:
sed -i 's/^port=.*/port=8080/' settings.confThe pattern .* matches everything that follows port=, so any old value gets replaced.
Key Takeaways
sed 's/old/new/'replaces text, and thegflag replaces all matches.-isaves edits to the file, and-i.bakkeeps a backup.ddeletes lines andpwith-nprints chosen lines.- Line numbers and patterns both select where a rule applies.
- Choose a separator other than
/when working with paths.
