100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

sed Quick Reference

A compact, scannable cheat sheet of sed's essential commands, addresses, flags, and regex constructs for fast day-to-day lookup.

PracticeBeginner7 min readJul 10, 2026
Analogies

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.

bash
# --- 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 numbers

Useful 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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#SedQuickReference#Sed#Quick#Reference#Core#StudyNotes#SkillVeris#ExamPrep