sed flags & options

-i

Edit files in-place. On macOS, use -i '' (empty string backup suffix).

sed -i 's/foo/bar/g' file.txt
sed -i.bak 's/foo/bar/g' file.txt

-n

Suppress automatic printing of lines. Only print when explicitly told with p.

sed -n '5p' file.txt
sed -n '10,20p' file.txt

-e

Add multiple editing commands in one invocation.

sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt

-E, -r

Use extended regular expressions. -E is portable, -r is GNU-only.

sed -E 's/[0-9]{3}-[0-9]{4}/REDACTED/g' file.txt

s/find/replace/

Substitute first match on each line. Add g for all matches.

sed 's/http/https/' urls.txt
sed 's/http/https/g' urls.txt

d

Delete lines matching a pattern or range.

sed '/^#/d' config.txt
sed '1,5d' file.txt

p

Print matching lines. Usually combined with -n.

sed -n '/error/p' log.txt

a, i

Append (a) or insert (i) text after or before a line.

sed '3a\new line here' file.txt
sed '1i\# Header' file.txt

Address ranges

Apply commands to specific lines or patterns.

sed '10,20s/foo/bar/g' file.txt
sed '/START/,/END/d' file.txt