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.
# 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.txtThe -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.
# 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 lineThe 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
1. What does sed -n '/START/,/END/p' do?
2. Why might a line print twice when you use the p command?
3. Which command inserts a new line BEFORE a matched line?
4. What does the substitution sed 's/foo/bar/2' do?
5. Which idiom deletes all blank lines from a file?
Was this page helpful?
You May Also Like
sed Best Practices
Practical habits for writing sed one-liners and scripts that are readable, portable, and safe to run against real data.
Building a Text-Processing Script
Move from ad-hoc one-liners to a maintainable multi-command sed script file, using labels, the hold space, and -f to solve a real data-cleaning task.
sed Quick Reference
A compact, scannable cheat sheet of sed's essential commands, addresses, flags, and regex constructs for fast day-to-day lookup.
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