sed Cheat Sheet

sed stream editing patterns: substitutions, addresses, ranges, deletion, extended regular expressions, and hold-space workflows.

CLI & Shell
sed
text
stream-editor

sed reads text line by line, applies an editing script to its pattern space, and writes the result. It prints transformed output by default; use a separate output file or a careful in-place mode when changing files.

Substitution and flags

The s command replaces the first match on each selected line. Flags control repeated matches, case handling, printing, and a specific occurrence.

bash
sed 's/old/new/' input.txt
sed 's/old/new/g' input.txt
sed 's/error/warning/gi' input.txt
sed -n 's/[0-9][0-9]*/NUMBER/p' input.txt
sed 's#https://old.example#https://new.example#g' links.txt

The delimiter after s need not be /; choose #, |, or another character when the pattern contains slashes. In a replacement, & means the complete match and \1 through \9 refer to captured groups.

bash
printf '%s\n' 'name=ada' | sed 's/^\([^=]*\)=\(.*\)$/\1 -> \2/'
printf '%s\n' 'id=42' | sed 's/[0-9][0-9]*/[&]/'

Addresses and ranges

A command can target every line, a line number, a regular expression, or an inclusive range between two addresses. Address 0 is useful with GNU sed for some range patterns but is not portable to BSD sed.

bash
sed '5p' input.txt
sed '/^ERROR/p' input.txt
sed '10,20s/[[:space:]]\+$//' input.txt
sed '/BEGIN/,/END/d' input.txt
sed '1,/^$/d' input.txt

Common address forms include 1, $, /pattern/, first~step in GNU sed, and addr,+N in GNU sed. For portable scripts, prefer line numbers, regular expressions, and ordinary two-address ranges.

Printing, deletion, and scripts

Use -n to suppress automatic printing. p explicitly prints the pattern space and d deletes it, ending the current cycle. Multiple -e expressions run in order.

bash
sed -n '1,12p' README.md
sed '/^[[:space:]]*#/d' config.ini
sed -e 's/[[:space:]]\+$//' -e '/^$/d' input.txt
sed -f edits.sed input.txt

A script file keeps complex transformations readable:

sed
s/[[:space:]]\+$//
/^$/d

In-place editing and regex modes

GNU and BSD/macOS sed differ in the argument required by -i. macOS uses an explicit empty backup suffix as -i ''; GNU sed accepts -i and -i'' for no backup. Supplying a suffix creates a backup such as file.txt.bak.

bash
sed -i '' 's/localhost/127.0.0.1/g' config.txt
sed -i.bak 's/localhost/127.0.0.1/g' config.txt
# GNU sed
sed -i 's/localhost/127.0.0.1/g' config.txt

Basic regular expressions are the default. -E enables extended regular expressions on GNU and BSD/macOS sed, reducing some backslash requirements for groups and alternation.

bash
sed -E 's/(error|failed):[[:space:]]*/warning: /' log.txt

Pattern and hold spaces

The pattern space is the current working buffer. The hold space is a second buffer for carrying text between cycles. h and H copy or append to hold; g and G retrieve or append from hold; x swaps the two spaces.

bash
sed -n '1h; 1!H; ${x;p;}' paragraphs.txt
sed -n '/BEGIN/{h;d}; /END/{x;p;d}' blocks.txt

These commands are useful for reversing, joining, or correlating nearby lines, but they are less transparent than a small script in another language for large transformations.

Files, escaping, and portability

r file reads a file after the current line; w file writes selected pattern-space output. Escape the delimiter, backslash, and literal ampersand when they are data. Shell quoting is a separate layer from sed’s own syntax.

bash
sed '/^---$/r footer.txt' document.md
sed -n '/^WARN/w warnings.log' application.log
sed 's/[&/]/\\&/g' input.txt

Use character classes such as [[:digit:]] and [[:space:]] when locale-aware behavior matters. Test a command without -i, inspect the output, then choose an explicit backup suffix for production edits.

References