How to Use This Reference
This page is a fast-lookup cheat sheet, not a tutorial: it groups sed's most-used commands, addressing forms, substitution flags, and regex metacharacters so you can find the right syntax in seconds. Every sed invocation follows the same shape — sed [options] 'address command' file — so once you can read that skeleton, each row below slots into place. Keep it open beside your terminal and copy the pattern nearest your task.
Cricket analogy: This cheat sheet is a batter's dismissal chart pinned in the dressing room — a glance tells you the field for each bowler without re-deriving the plan.
Core Commands and Addresses
The command you will use most is s for substitute, followed by d (delete), p (print), and the line-adders i (insert), a (append), and c (change). Each is gated by an optional address that picks which lines it runs on: a line number like 5, a range like 2,8, a pattern like /error/, the last-line symbol $, or GNU's step form first~step (e.g. 1~2 for odd lines). Prefixing an address with ! negates it, so /debug/!d deletes every line that does NOT contain 'debug'.
Cricket analogy: Addresses are field placements deciding where a command applies; the ! negation is like inverting the field to attack the batter's weaker side instead.
# --- SUBSTITUTION ---
sed 's/old/new/' # first match per line
sed 's/old/new/g' # all matches per line
sed 's/old/new/3' # 3rd match per line
sed 's/old/new/gI' # all, case-insensitive (GNU)
sed 's/\(a\)\(b\)/\2\1/' # swap groups via backreferences
# --- ADDRESSES ---
sed '5d' # delete line 5
sed '2,8d' # delete lines 2-8
sed '/error/d' # delete lines matching /error/
sed '$d' # delete last line
sed '1~2d' # delete odd lines (GNU step)
sed '/keep/!d' # delete lines NOT matching /keep/
# --- OTHER COMMANDS ---
sed -n '/x/p' # print matches (with -n)
sed '3a appended' # append after line 3
sed '3i inserted' # insert before line 3
sed '3c replaced' # change line 3 entirely
sed '=' file # print line numbersUseful options at a glance: -n suppresses default printing; -e adds another command (stack several); -f loads a script file; -i edits in place (add a suffix for a backup); -r or -E enables extended regex so you can drop the backslashes before ( ) { } + | in your patterns.
Regex Metacharacters and Replacement Escapes
In sed's default Basic Regular Expressions (BRE), ^ and $ anchor start and end, . matches any character, * repeats the previous atom, and [set] is a character class. Crucially, in BRE the grouping and interval operators must be escaped: \( \), \{ \}, and \+ — but with -E (extended regex) you write them bare. In the replacement, & inserts the whole match, \1 through \9 insert captured groups, and \n, \t, \U, \L (GNU) produce newlines, tabs, and case conversions.
Cricket analogy: The & in the replacement reusing the whole match is like a batter reusing the same successful stroke, while \1 replays just one component of that shot.
The single biggest BRE gotcha: + and ? and | and ( ) and { } are LITERAL characters in basic regex unless backslash-escaped. So sed 's/ab+/X/' looks for 'ab+' literally, not 'one a and one-or-more b'. Either escape as \+ or switch to extended regex with -E (sed -E 's/ab+/X/'). Mixing the two conventions is a frequent source of patterns that silently never match.
- Every sed call fits sed [options] 'address command' file — learn the skeleton, slot in the rest.
- Top commands: s (substitute), d (delete), p (print), i/a/c (insert/append/change).
- Addresses: line number, range (2,8), /pattern/, $ (last), 1~2 (GNU step); ! negates.
- Substitution flags: g (all), a number (Nth), I (case-insensitive, GNU), p (print).
- Options: -n (suppress), -e (add command), -f (script file), -i (in-place), -E/-r (extended regex).
- In BRE, ( ) { } + must be escaped; -E lets you write them bare.
- In the replacement, & is the whole match and \1-\9 are captured groups.
Practice what you learned
1. What does the address form 1~2 mean in GNU sed?
2. In the replacement text, what does & represent?
3. Why does sed 's/ab+/X/' fail to match 'abbb' in default mode?
4. What does the ! after an address do, as in /debug/!d?
5. Which option lets you write ( ) and + without backslashes in patterns?
Was this page helpful?
You May Also Like
Common sed Idioms
A curated set of battle-tested sed one-liners for deletion, printing ranges, in-line insertion, and squeezing blank lines that you will reach for again and again.
sed Best Practices
Practical habits for writing sed one-liners and scripts that are readable, portable, and safe to run against real data.
sed Interview Questions
The concepts interviewers probe most — the difference between sed and grep/awk, in-place editing, greedy matching, and multi-line handling — framed as questions with model answers.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics