Skip to content

Menu

sed Cheat Sheet: Substitution, Addresses, In-Place Edits and Hold Space

Checked against sed GNU sed 4.10 manual (Texinfo, 20 April 2026); examples run with macOS BSD sed on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for developers who reach for sed to fix a config value, delete lines or reshape text in a pipeline. It was checked against the GNU sed 4.10 manual, which I read from the Texinfo source in the release tarball on ftp.gnu.org because www.gnu.org was down on 28 November 2026. I ran the examples with the BSD sed in macOS; rows that rely on GNU extensions are marked untested.

When to use what

Most sed use is one substitution: s/old/new/ for the first match on each line, s/old/new/g for all of them. Add -E whenever your pattern has groups or +, because the basic syntax needs \(, \) and friends and is much harder to read. Both GNU and macOS sed accept -E, so it's the portable choice.

In-place editing is where GNU and macOS split. GNU sed takes an optional suffix glued to the flag: -i edits with no backup and -i.bak keeps one. macOS sed wants a separate argument, so you write -i '' for no backup. When I ran the GNU form sed -i 's/1/2/' c2.txt on macOS, sed took the script as the backup suffix, read the file name as the script and failed with an error about the c command. -i.bak with no space works on both, so that's what I use in scripts meant for both systems, deleting the .bak afterwards.

Once a job needs field splitting or arithmetic, switch to awk; once it needs state across many lines, a short script in a real language is easier to maintain than hold-space tricks. Test patterns in the regex tester, use the regex escape tool when the text you're replacing is full of dots and slashes, and compare before and after with the text diff checker. Our regex patterns post has patterns you can drop into s///.

In the replacement, & is the whole match and \1 to \9 are groups. For a literal &, backslash or newline in the replacement, put a backslash in front of it.

echo 'a-a-a' | sed 's/a/b/'

Replace the first match on each line only (gives b-a-a)

echo 'a-a-a' | sed 's/a/b/g'

Replace every match on the line (gives b-b-b)

echo 'a-a-a' | sed 's/a/b/2'

Replace only the second match (gives a-b-a)

echo 'id42' | sed 's/[0-9][0-9]*/[&]/'

Wrap the match using & (gives id[42])

echo '2026-11-28' | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3.\2.\1/'

Reorder groups with extended regex (gives 28.11.2026)

echo '2026-11-28' | sed 's/\([0-9]*\)-\([0-9]*\)-\([0-9]*\)/\3.\2.\1/'

Same thing in basic regex, with escaped parentheses

echo '/usr/local/bin' | sed 's|/usr/local|/opt|'

Use a different delimiter so slashes need no escaping

echo 'foo bar' | sed -e 's/foo/F/' -e 's/bar/B/'

Chain several commands with repeated -e

Flags go after the closing delimiter and can be stacked, as in s/x/y/gI. GNU sed also accepts \U, \L, \u, \l and \E in the replacement to change case; macOS sed has none of them. With g, a case change ends with each replacement and never spills into the next match. The e flag runs whatever ends up in the pattern space as a shell command, so keep it away from input you didn't write.

echo 'a-a-a-a' | sed 's/a/b/2g'

GNU: replace the second match and every one after it (gives a-b-b-b)

macOS sed refused to mix a number with g.

From the docs, not run by us
printf 'x1\ny\n' | sed -n 's/1/2/p'

Print only the lines where a substitution happened (gives x2)

printf 'v=1\nw=1\nz\n' | sed -n 's/1/2/w changed.txt'

Also write each changed line to changed.txt, which ends up holding v=2 and w=2

echo 'Hello HELLO' | sed 's/hello/hi/Ig'

I (or i) matches without regard to case (gives hi hi)

The manual calls I a GNU extension, but macOS sed accepted it too.

printf 'a\nb\n' | sed 'N;s/^b/B/M'

GNU: M lets ^ and $ match next to newlines inside a multi-line pattern space

From the docs, not run by us
echo 'x' | sed 's/x/date +%Y/e'

GNU: run the result as a shell command and replace the line with its output

From the docs, not run by us
echo 'hello world' | sed -E 's/([a-z]+) ([a-z]+)/\u\1 \U\2/'

GNU: \u uppercases one character, \U everything after it (gives Hello WORLD)

From the docs, not run by us
echo 'foo bar' | sed -E 's/(foo) (bar)/\U\1\E \2/'

GNU: \E stops the conversion, so only the first word changes (gives FOO bar)

From the docs, not run by us
echo 'API_KEY Hello' | sed -E 's/([A-Z_]+) (.)/\L\1 \l\2/'

GNU: \L lowercases the rest, \l just the next character (gives api_key hello)

From the docs, not run by us
printf '1\n2\n3\n4\n' | sed -n '2,3p'

Print only lines 2 to 3 (-n turns off automatic printing)

printf '1\n2\n3\n' | sed -n '$p'

Print the last line

printf 'keep\n# c\nkeep2\n' | sed '/^#/d'

Delete lines matching a regex

printf 'a\nb\nc\n' | sed -n '/b/!p'

! negates the address: print lines that don't match

printf 'x\nSTART\nm\nEND\ny\n' | sed -n '/START/,/END/p'

Print an inclusive block between two patterns

sed -n '1~2p' file

GNU: print odd-numbered lines (first~step)

From the docs, not run by us
sed '/error/,+2d' file

GNU: delete a matching line and the two after it

From the docs, not run by us
sed '0,/foo/s//bar/' file

GNU: replace only the first foo in the whole file

From the docs, not run by us
printf '1\n2\n3\n' | sed 2q

Print up to line 2 then quit, like head -2

echo 'abc' | sed 'y/abc/xyz/'

Transliterate characters one-for-one (gives xyz)

printf 'a\nb\n' | sed -n '$='

Print the number of the last line, which is the line count

sed 'Q' file

GNU: quit without printing the current line

From the docs, not run by us
sed -s -n '$p' a.txt b.txt

GNU: treat files separately, printing each file's last line

From the docs, not run by us
sed --debug 's/a/b/' file

GNU: annotate how the script runs, line by line

From the docs, not run by us
sed -i '' 's/1/2/' conf.txt

macOS: edit the file in place with no backup

sed -i.bak 's/2/3/' conf.txt

Edit in place and keep conf.txt.bak; works on both GNU and macOS

sed -i 's/1/2/' conf.txt

GNU: edit in place with no backup; on macOS this fails or misreads arguments

From the docs, not run by us
sed -i --follow-symlinks 's/a/b/' link.conf

GNU: edit the file a symlink points to instead of replacing the link

From the docs, not run by us

h/H copy or append the pattern space to the hold space, g/G copy or append it back, x swaps them, and N appends the next input line.

printf 'a\nb\n' | sed G

Double-space: append the empty hold space after every line

printf '1\n2\n3\n' | sed -n '1!G;h;$p'

Reverse the order of lines, like tac

printf 'k:\nv\n' | sed 'N;s/\n/ /'

Join each pair of lines into one (gives k: v)

Worked example: Change a config value in place on macOS and Linux

I wanted one command that bumps v=1 in a config file and works for teammates on both systems. This is what macOS sed did with each form.

$ printf 'v=1\n' > conf.txt
$ sed -i '' 's/1/2/' conf.txt; cat conf.txt
v=2
$ sed -i.bak 's/2/3/' conf.txt; cat conf.txt conf.txt.bak
v=3
v=2
$ printf 'v=1\n' > c2.txt
$ sed -i 's/1/2/' c2.txt
sed: 1: "c2.txt
": command c expects \ followed by text

The empty-string form works on macOS, but GNU sed only accepts a suffix glued to -i, so it would not treat '' as one. The -i.bak form worked here and GNU's -i[SUFFIX] syntax accepts it too, and the .bak file holds the previous value, so you can check the change before deleting it. The last command shows the GNU habit failing on macOS: BSD sed used s/1/2/ as the backup suffix and then tried to run the file name as a script, which starts with the c command. For shared scripts, use sed -i.bak ... && rm file.bak.

Common mistakes

  • Running sed -i 's/x/y/' file from a Linux tutorial on macOS.

    BSD sed needs a suffix argument. Use sed -i '' ... on macOS or -i.bak everywhere.

  • Writing sed 's/(foo)+/\1/' without -E and getting no change.

    Basic regex treats ( and + as literals. Add -E for extended syntax.

  • Using \U\1 in a replacement to uppercase a group on macOS.

    The case escapes \U, \L, \u, \l and \E are GNU extensions, and macOS sed printed a literal U when I tried one. Use GNU sed, or change case with tr or awk.

  • Escaping every slash in a path, as in s/\/usr\/local/\/opt/.

    Pick another delimiter: s|/usr/local|/opt| reads far better and is less error-prone.

  • Forgetting -n with p, so every selected line prints twice.

    p prints on top of sed's automatic printing. Add -n when you only want the lines p selects.

  • Writing s/a/b/2g on a Mac to change everything from the second match on.

    BSD sed rejects a number combined with g. Run it with GNU sed, or loop with a label and t if it must work on both.