📖 Guide
Sed — Complete Reference
Comprehensive sed cheat sheet covering substitution, address ranges, transforms, hold space, branching, and common one-liners.
54 commands across 8 categories
Basic Substitution
| Command | Description |
|---|---|
sed 's/old/new/' file | Replace first occurrence of 'old' with 'new' on each line |
sed 's/old/new/g' file | Replace all occurrences of 'old' with 'new' on each line |
sed -i 's/old/new/g' file | Edit file in-place (modifies the original file) |
sed -i.bak 's/old/new/g' file | Edit in-place with backup (.bak extension) |
sed 's|/usr/bin|/usr/local/bin|g' file | Use alternate delimiter (| instead of /) for paths |
sed 's/old/new/2' file | Replace only the 2nd occurrence on each line |
sed 's/old/new/gi' file | Case-insensitive replacement (GNU sed) |
sed 's/.*://' file | Delete everything up to and including the last colon |
Address Ranges
| Command | Description |
|---|---|
sed '3s/old/new/' file | Replace only on line 3 |
sed '2,5s/old/new/' file | Replace on lines 2 through 5 |
sed '/pattern/s/old/new/' file | Replace only on lines matching pattern |
sed '1,/pattern/s/old/new/' file | Replace from line 1 to first line matching pattern |
sed '/start/,/end/s/old/new/' file | Replace between lines matching start and end patterns |
sed '$ s/old/new/' file | Replace only on the last line |
sed '0~2s/old/new/' file | Replace on every 2nd line (GNU sed: start~step) |
Delete & Insert
| Command | Description |
|---|---|
sed '3d' file | Delete line 3 |
sed '/pattern/d' file | Delete all lines matching pattern |
sed '2,5d' file | Delete lines 2 through 5 |
sed '/^$/d' file | Delete all blank lines |
sed '3i\inserted text' file | Insert text before line 3 |
sed '3a\appended text' file | Append text after line 3 |
sed '3c\replacement line' file | Replace entire line 3 with new text |
sed '/pattern/r other.txt' file | Read and insert contents of other.txt after matching lines |
Transform
| Command | Description |
|---|---|
sed 'y/abc/ABC/' file | Transliterate characters (a→A, b→B, c→C) |
sed 's/.*/\U&/' file | Convert entire line to uppercase (GNU sed) |
sed 's/.*/\L&/' file | Convert entire line to lowercase (GNU sed) |
sed 's/\b./\u&/g' file | Capitalize first letter of every word (GNU sed) |
sed 's/[[:space:]]*$//' file | Remove trailing whitespace from each line |
sed 's/^[[:space:]]*//' file | Remove leading whitespace from each line |
Hold Space
| Command | Description |
|---|---|
h | Copy pattern space to hold space (overwrite) |
H | Append pattern space to hold space |
g | Copy hold space to pattern space (overwrite) |
G | Append hold space to pattern space |
x | Exchange pattern space and hold space |
sed -n '1!G;h;$p' filee.g. Uses hold space to accumulate lines in reverse | Reverse line order of file (like tac) |
Branching
| Command | Description |
|---|---|
:label | Define a label for branching |
b label | Branch (jump) to label unconditionally |
t label | Branch to label only if last s/// succeeded |
T label | Branch to label only if last s/// did NOT succeed (GNU sed) |
sed ':a;N;$!ba;s/\n/ /g' filee.g. Loops with :a label until last line, then substitutes | Join all lines into one (replace newlines with spaces) |
Flags
| Command | Description |
|---|---|
sed -n 'p' file | -n suppresses auto-print; only explicit p prints |
sed -e 'cmd1' -e 'cmd2' file | -e allows multiple commands |
sed -f script.sed file | -f reads commands from a script file |
sed -E 's/(group)/\1/' file | -E enables extended regex (ERE) for cleaner syntax |
sed -n 's/pattern/replace/p' file | Print only lines where substitution was made |
sed -n 's/pattern/replace/w output.txt' file | Write lines where substitution was made to output.txt |
Common One-liners
| Command | Description |
|---|---|
sed -n '5,10p' file | Print lines 5 through 10 |
sed -n '1p' file | Print only the first line (like head -1) |
sed 'G' file | Double-space a file (insert blank line after each line) |
sed '/^#/d' file | Remove comment lines (starting with #) |
sed 's/<[^>]*>//g' file | Strip HTML tags |
sed -n '/pattern/=' file | Print line numbers of matching lines |
sed '=' file | sed 'N;s/\n/\t/' | Number all lines with a tab separator |
sed '$!N;/^\(.*\)\n\1$/!P;D' file | Remove consecutive duplicate lines (like uniq) |
📖 Free, searchable command reference. Bookmark this page for quick access.