From Building Blocks to Recipes
Once you know substitution, addresses, and a few commands like d, p, and n, most real-world edits become short compositions of these primitives. A recipe is simply a reliable pattern you reach for repeatedly: deleting blank lines, printing a range between two markers, or reformatting a date. Keeping a mental (or literal) cookbook saves you from reinventing tricky escaping each time and reduces errors on files you cannot easily re-generate. The recipes below are portable across GNU and BSD sed unless noted.
Cricket analogy: Like a batter's set of go-to shots — the cover drive, the pull, the late cut — each recipe is a rehearsed stroke you play on instinct when the delivery (edit) calls for it.
Deleting and Selecting Lines
The d command deletes lines matching an address, and combined with -n and p you can invert it to keep only what you want. To delete blank lines use 'sed /^$/d'. To delete a range use line-number or pattern addresses, for example 'sed 2,5d' or 'sed /BEGIN/,/END/d'. To print only a range, suppress default output with -n and append p to the address: 'sed -n /START/,/END/p'. The comma between two addresses defines an inclusive range from the first match to the next line matching the second pattern.
Cricket analogy: Deleting blank lines is like the umpire signaling dead balls out of the tally — 'sed 2,5d' removes overs 2 through 5 from the scorecard as if they never happened.
# Delete blank lines
sed '/^$/d' file.txt
# Squeeze multiple blank lines into one (portable)
sed '/^$/{ N; /^\n$/D }' file.txt
# Print only lines between two markers (inclusive)
sed -n '/-- BEGIN --/,/-- END --/p' report.txt
# Reformat MM/DD/YYYY -> YYYY-MM-DD using capture groups (GNU)
sed -E 's#([0-9]{2})/([0-9]{2})/([0-9]{4})#\3-\1-\2#g' dates.txt
# Join every line to the next (unwrap) — pair lines with N
sed 'N;s/\n/ /' wrapped.txt
# Number non-blank lines
sed '/./=' file.txt | sed '/^[0-9]/N; s/\n/\t/'sed lets you use any character as the substitution delimiter — 's#a#b#' works as well as 's/a/b/'. When your pattern or replacement contains slashes (like file paths or dates), switch the delimiter to # or | to avoid a thicket of backslash-escaped slashes. This dramatically improves readability.
Reformatting with Capture Groups
Backreferences turn sed into a lightweight reformatter. Wrap parts of the pattern in groups and reference them in the replacement as \1, \2, and so on. In BRE the groups are '\(...\)'; with -E they are plain '(...)'. Reordering date components, swapping 'Last, First' into 'First Last', or wrapping matched text in markup are all a single substitution. The special replacement '&' inserts the entire match, useful for wrapping without capturing, as in 's/[0-9]+/[&]/' to bracket every number.
Cricket analogy: Capture groups are like reordering a batting lineup card — grab the opener (\1) and number three (\2) and print them in a new order, exactly as \3-\1-\2 rearranges a date.
Backreference numbering differs by dialect. In BRE, capturing groups are '\( \)' and references are \1..\9. Forgetting the -E flag while using unescaped parentheses means sed treats '(' as a literal character, so \1 refers to nothing and your reformat silently fails. Match your group syntax to your regex mode.
- Most edits compose from primitives: substitution, addresses, and d/p/n commands.
- 'sed /^$/d' deletes blank lines; 'sed 2,5d' and 'sed /A/,/B/d' delete ranges.
- Use -n with p to print only selected lines or ranges (inclusive between two addresses).
- Change the substitution delimiter (s#a#b#) to avoid escaping slashes in paths and dates.
- Capture groups plus \1, \2 backreferences reformat and reorder matched text.
- '&' in the replacement inserts the entire match, ideal for wrapping text.
- Group syntax depends on dialect: '\(...\)' in BRE, '(...)' with -E.
Practice what you learned
1. Which command deletes all blank lines from a file?
2. What does '&' represent in a sed replacement string?
3. Why change the substitution delimiter to '#', as in 's#/usr#/opt#'?
4. How do you print only the lines between markers BEGIN and END?
5. In BRE mode, how is a capturing group written?
Was this page helpful?
You May Also Like
sed in Shell Pipelines
How to wire sed into Unix pipelines as a stream filter, combining it with cat, grep, sort, and xargs to build powerful one-liner text-processing workflows.
Config File Editing with sed
Using sed to safely automate edits to configuration files — in-place editing with backups, targeting keys precisely, and pitfalls when scripting changes to production configs.
sed vs awk vs grep
A comparison of the three classic Unix text tools — when to reach for grep to search, sed to edit streams, and awk for field-oriented and programmable processing.
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