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

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.

PracticeIntermediate10 min readJul 10, 2026
Analogies

The Vocabulary of Everyday sed

Most real sed usage is not exotic scripting but a handful of recurring idioms: delete matching lines, print only a range, insert text before a marker, and collapse repeated blanks. Learning these as fixed phrases — the way you memorize common chords rather than deriving them each time — turns sed from a puzzle into a fast tool. Each idiom combines an address (which lines) with a command (what to do), and the power comes from mixing the two fluently.

🏏

Cricket analogy: These idioms are your bread-and-butter shots — the forward defensive and the cover drive — that a batter plays automatically without rethinking the mechanics each ball.

Deleting and Printing by Address

The delete command d removes lines that match an address. Use /pattern/d to drop every line containing a pattern, or a line-range like 2,5d to remove specific lines. Its mirror image is the print idiom: because sed prints every line by default, printing selectively means suppressing that default with -n and then explicitly printing with p. The classic sed -n '/start/,/end/p' prints an inclusive range between two matching patterns — a workhorse for extracting a stanza from a log or config.

🏏

Cricket analogy: sed -n '/start/,/end/p' extracting a range is like a highlights package clipping from the first wicket to the drinks break, discarding the rest of the innings.

bash
# Delete every line containing 'DEBUG'
sed '/DEBUG/d' app.log

# Delete lines 2 through 5 inclusive
sed '2,5d' file.txt

# Print ONLY the block between two markers (inclusive)
sed -n '/BEGIN CONFIG/,/END CONFIG/p' server.conf

# Print only line 42
sed -n '42p' file.txt

# Delete blank lines
sed '/^$/d' file.txt

The -n flag is the switch that turns sed from a 'print everything' tool into a 'grep with superpowers' extractor. Whenever you see -n paired with an explicit p, read it as: suppress automatic output, then hand-pick exactly what to emit. Forgetting -n while using p is the reason lines print twice — a classic beginner surprise.

Insert, Append, and Squeeze Blanks

The insert (i) and append (a) commands add whole new lines relative to a matched address: i places text before the matched line, a places it after. GNU sed accepts the compact one-line forms i text and a text. Another daily idiom is squeezing runs of blank lines into a single blank with cat -s-like behavior: sed '/^$/N;/^\n$/D' collapses consecutive empties. These structural edits — inserting a header, appending a footer, tidying spacing — are exactly the jobs sed handles more cleanly than a full editor.

🏏

Cricket analogy: Inserting a line before a match is like the umpire signaling a new over before the first ball; appending is adding the wide runs after the delivery.

bash
# Insert a line BEFORE the first line (a header)
sed '1i # Generated file — do not edit' data.csv

# Append a line AFTER every line matching 'END'
sed '/END/a ---- section break ----' report.txt

# Squeeze runs of blank lines down to one
sed '/^$/N;/^\n$/D' messy.txt

# Replace only the FIRST match on each line (default s behaviour)
sed 's/foo/bar/' file        # first per line
sed 's/foo/bar/g' file       # every match on the line
sed 's/foo/bar/2' file       # only the 2nd match per line

The i and a commands in POSIX sed traditionally require a backslash-newline before the text, and any literal newlines in the inserted text must be escaped with a backslash. The compact 1i text one-line form is a GNU extension — if your script must run on BSD/macOS or strict POSIX sed, use the multi-line i\ form or you will get syntax errors.

  • /pattern/d deletes matching lines; 2,5d deletes a line-number range.
  • sed -n with explicit p prints only selected lines, turning sed into a precise extractor.
  • sed -n '/start/,/end/p' prints an inclusive block between two pattern markers.
  • i inserts a line before a match, a appends one after; the compact one-line form is a GNU extension.
  • sed '/^$/d' removes blank lines and '/^$/N;/^\n$/D' squeezes runs into a single blank.
  • s/// replaces the first match per line; add g for all, or a number for the Nth match.
  • Forgetting -n while using p causes lines to print twice — a common beginner error.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#CommonSedIdioms#Common#Sed#Idioms#Vocabulary#StudyNotes#SkillVeris#ExamPrep