sed - stream editor for filtering and transforming text
sed [-n] [-i[SUFFIX]] [-E] SCRIPT [file]...
Options you will use
-n- Suppress automatic printing; print only what p asks for.
-i[SUFFIX]- Edit files in place. -i.bak keeps a backup - do that on anything under /etc.
-E, -r- Extended regular expressions.
-e SCRIPT- Add another script; use it more than once for multiple commands.
s/RE/REPL/[gi]- Substitute. g = every occurrence on the line, i = case-insensitive. & is the whole match, \1 a capture group.
ADDR{,ADDR}cmd- Address: a line number, $ for last, /regex/, or a range like 3,6 or /start/,/end/.
d p q = a i c y- delete, print, quit, print line number, append, insert, change, transliterate.
-i[SUFFIX], --in-place[=SUFFIX]- Edit the file in place; with a SUFFIX (-i.bak) keep a backup copy first.
Examples
$ sudo sed -i.bak 's/read.timeout.ms=0/read.timeout.ms=5000/' /etc/orders/app.confin-place edit with a backup
$ sed -n '3,6p' fileprint lines 3 to 6
$ sed '/^#/d;/^$/d' /etc/fstabstrip comments and blank lines
$ sed -i 's|^/swap.img|#&|' /etc/fstabcomment out a line; | as the delimiter avoids escaping slashes
Gotchas
- sed -i needs write permission on the DIRECTORY (it writes a temp file and renames it), not just on the file.
Taught in
Try sed in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.