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

Text Transformation Recipes

A practical cookbook of proven sed one-liners for everyday text surgery: deleting lines, extracting ranges, joining lines, and reformatting with capture groups.

Practical sedIntermediate10 min readJul 10, 2026
Analogies

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.

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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#TextTransformationRecipes#Text#Transformation#Recipes#Building#StudyNotes#SkillVeris#ExamPrep