Substitute
sed 's/<re>/<new>/' <file>
Replace first match on each line
sed 's/<re>/<new>/g' <file>
Replace every match
sed 's/<re>/<new>/<N>'
<file>
Replace only the Nth match
sed 's/<re>/<new>/gI'
<file>
Ignore case
sed 's/<re>/&!/' <file>
& inserts the whole match
sed 's|<path>|<new>|g'
<file>
Pick another delimiter for slashes
sed -E 's/(<a>)(<b>)/\2\1/'
<file>
Swap groups with backreferences
sed -E 's/<re>/\U&/' <file>
Uppercase the match (\L lower, \u first)
\E ends a \U or \L span. GNU only.
sed 's/<re>/<new>/w <out>'
<file>
Write changed lines to a file
sed 's/<re>/<new>/;t;s/^/NO: /'
<file>
Branch if a substitution happened
Options
sed -n
Print only when told to (p)
sed -i[<suffix>]
Edit in place, optional backup
-i.bak keeps <file>.bak. Do not put a space between -i
and the suffix.
sed -E
Extended regex: (), +, ?, |
sed -e '<cmd>' -e '<cmd>'
Combine several commands
sed -f <script.sed> <file>
Read commands from a file
sed -s
Treat each file as separate (for $, 1)
sed -z
Split input on NUL, not newline
With -z a whole file is one line, so s/\n/ /g works across
lines.
sed --debug
Show the parsed script and each step
sed --posix
Disable GNU extensions
sed -u
Unbuffered, for pipes that stream
Commands
p
Print the pattern space
d
Delete line, start next cycle
q / Q
Quit (Q without printing)
a\ <text>
Append text after the line
i\ <text>
Insert text before the line
c\ <text>
Replace the line with text
y/<abc>/<xyz>/
Transliterate characters
r <file>
Append a file's content
w <file>
Write the line to a file
l
Show line unambiguously (\t, $)
n / N
Next line (replace / append)
{ cmd; cmd }
Group commands under one address
h / H / g / G / x
Hold space: copy / append / get / get-append / swap
D / P
Delete / print up to first newline
b <label> / t <label> / :<label>
Branch / branch if substituted / label