-i – Modify and save the specified document.
Replace foo with too in the first line found.
$ sed -i ‘s/foo/too/’
Replace foo with too throughout the entire file.
$ sed -i ‘s/foo/too/g’
Delete the 14th line.
$ sed -i ‘14d’ /file
Delete the line containing Network or network.
$ sed -i ‘/[Nn]etwork/d’ /file
Add “[mounts]” to the end of the file, then a new line and “user root”.
$ sed -i ‘$ a \\n[mounts]\nuser root’ /etc/munin/config
After the 14th line, add “echo ”graph_category logger“”.
$ sed ‘14a\ echo \“graph_category logger\”’ /etc/munin/plugins/command
Insert ‘# vim: ft=ruby’ and a line break at the beginning of the file.
$ sed ‘1i # vim: ft=ruby\n’
Replace repeated empty lines with a single empty line.
$ sed -e ‘:a;/^$/N;/\n$/{D;ba}’ file.txt
Convert DOS line breaks (CR/LF) to Unix (LF)
$ sed ‘s/.$//’ dosfile.txt > unixfile.txt
Replace string1 with string2
$ sed ‘s/string1/string2/g’
Change string anystring1 to anystring2
$ sed ‘s/\(.*\)1/\12/g’
Remove comments and empty lines
$ sed ‘/ *#/d; /^ *$/d’
sed -i “/^$/d” file.txt #-i for changes within the document
Join strings (lines) with preceding \
$ sed ‘:a; /\\$/N; s/\\\n//; ta’
Remove preceding spaces from strings
$ sed ‘s/[ \t]*$//’
Escape active shell metacharacters with double quotes
$ sed ‘s/\([\\`\\”$\\\\]\)/\\\1/g’
Align numbers to the right margin
$ seq 10 | sed “s/^/ /; s/ *\(.\{7,\}\)/\1/”
Print the 1000th line
$ sed -n ‘1000p;1000q’
Print lines 10 through 20
$ sed -n ‘10,20p;20q’
Get the title from an HTML page
$ sed -n ‘s/.*
Partially taken from here, thanks to the author!