Lesson 10
sed Editing Commands: Review and Next Steps
Objective: Review selection, substitution, quoting, and safe output handling.
You can now use sed to transform a text stream reproducibly. The central ideas are input addresses, pattern space, ordered instructions, and the difference between explicit and automatic printing. Those ideas explain the examples more reliably than memorizing isolated commands.
| Task | Example |
|---|---|
| Delete matching records | sed '/^M/d' petchip |
| Print a range | sed -n '2,5p' petchip |
| Replace every colon | sed 's/:/#/g' petchip |
| Reuse matched text | sed 's/[0-9]\{5\}$/ID#&/' petchip |
Before using a transformation on real data
- Use straight shell quotes and understand which language interprets each special character.
- Test on representative input, including empty fields and nonmatching lines.
- Inspect the output and exit status.
- Write to a separate file; preserve a backup before an intentional replacement.
- Distinguish portable sed syntax from implementation-specific features such as in-place editing.
Terms to retain
Regular expression: a pattern describing text to match. Pattern space: sed’s working buffer. Redirection: routing a command’s input or output to a file. Pipeline: connecting commands through streams. Replacement: the text and substitution tokens used to construct a successful match’s new value.
Sed can reformat text; it is incorrect to claim that formatting is impossible. Awk is often clearer for field-oriented reporting, arithmetic, and formatted records. The next module develops that complementary approach.