Terminal · intermediate
Stream editing with sed
sed applies an ordered editing program to a stream of input records, using addresses to select records and commands to transform or emit them.
Why it matters
It handles repeatable line-oriented transformations without an interactive editor, but regex dialects, delimiters, and in-place options vary across implementations.
Mental model
How to reason about stream editing with sed
Read one input line into a pattern space, run the script's applicable commands in order, normally print the resulting pattern space, then continue with the next line.
Analogy
It is a document conveyor with a fixed instruction card applied to each passing line before that line leaves the station.
Examples
See the boundary, not just the happy path
Worked example · Replace every occurrence
sed 's/http:/https:/g' links.txts performs substitution and g repeats it across each pattern space; without g, only the first match on each line changes.
Worked example · Print a selected range
sed -n '20,30p' server.log-n disables default printing and the addressed p command emits only lines 20 through 30.
Useful contrast · Transform output, not the source
sed 's/dev/prod/' config.txt > config.newOrdinary sed writes transformed text to stdout and leaves config.txt unchanged; replacement requires an explicit verified file update workflow.
Common mistakes
Misconceptions to remove early
Assuming -i syntax is portable
GNU and BSD sed differ in in-place option details; use a temporary output plus verified rename when portability and recovery matter.
Forgetting replacement metacharacters
In the replacement, & means the entire match and backslashes have special roles, so arbitrary replacement data requires careful escaping.
Quick check
Can you predict the result?
1. What does the g flag do in sed 's/a/b/g'?
- • It replaces every non-overlapping match in each pattern space rather than only the first.
- • It searches directories recursively.
- • It edits the source file in place.
2. Why pair -n with an explicit p command?
Keep building