sed Commands Reference

All sed commands, addresses and flags with examples for stream editing.

Searchable sed command reference covering line and regex addressing, the substitute command and its g/N/p/i flags, append/insert/change/delete editing, hold-space and branching commands, plus command-line flags like -n, -i and -E. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What does the -n flag do in sed?

By default sed prints every line after processing. The -n flag suppresses that automatic printing, so output appears only where you explicitly use the p command or the p flag on a substitution. It is the basis of sed -n '5p' to print one line.

sed — stream editor reference

sed is a non-interactive stream editor: it reads input line by line into a buffer called the pattern space, applies a script of editing commands, and writes the result out. It excels at find-and-replace, deleting or printing selected lines, and multi-line transforms via the hold space. This page is a searchable, offline reference to sed’s addresses, commands, substitute flags and command-line options, each with a working example.

How it works

Every sed command may be prefixed with an address that selects which lines it applies to:

  • Addresses can be a line number 3, the last line $, a regex /re/, a range 2,5 or /a/,/b/, GNU step form 1~2, or a negated form addr!.
  • The substitute command s/regex/replacement/flags is the workhorse. Flags include g (all matches), a number N (the N-th match), p (print on change), i (case-insensitive) and m (multi-line). In the replacement, & is the whole match and \1..\9 are captured groups.
  • Editing commands add (a), insert (i), change (c), delete (d), print (p), transliterate (y) and quit (q).
  • Advanced commands move text between the pattern and hold spaces (h, H, g, G, x) and branch with labels (:label, b, t, T) for loops.

The output model matters: with no -n, sed prints the pattern space at the end of each cycle; with -n it prints nothing unless you ask.

Practical examples

Delete blank lines and comment lines:

sed '/^$/d; /^#/d' config

Replace every tab with a comma across the whole file, in place with a backup:

sed -i.bak 's/\t/,/g' data.tsv

Print only lines 10 through 20:

sed -n '10,20p' file

Join all lines into one using the hold-space loop idiom:

sed ':a; N; $!ba; s/\n/ /g' file

Extract lines between two patterns (inclusive):

sed -n '/START/,/END/p' file

Add a line after every line matching a pattern:

sed '/pattern/a\new line to insert' file

BSD versus GNU sed: the key differences

macOS ships BSD sed, while Linux typically has GNU sed. The most common points of divergence:

FeatureGNU sedBSD sed (macOS)
In-place editsed -i 's/x/y/' filesed -i '' 's/x/y/' file
Extended regex-E or -r-E only
Step address1~2 (every other line)not supported
Multiline mode m flagsupportednot supported
\w in regexsupportednot supported

If a sed one-liner works on Linux but fails on macOS, check for GNU-only features and use gsed (install via Homebrew) on macOS to get GNU behaviour.

Common mistakes to avoid

  • Testing without -i: always run the command without -i first to verify the output, then add -i once confirmed. A wrong regex with -i overwrites the original file immediately.
  • Forgetting g flag: s/old/new/ only replaces the first occurrence per line. Add g for all occurrences.
  • Delimiter clashes: if your pattern contains /, either escape it as \/ or choose a different delimiter — s|/old/|/new/|g uses | instead and is much easier to read.
  • Hold-space confusion: h copies pattern space to hold space (overwriting it); H appends. g copies hold to pattern; G appends. Mixing these up silently drops lines.