sed Cheatsheet
Top patterns.
Overview
sed is the stream editor that performs text substitution and transformation. The cheatsheet captures the patterns operators actually use; fluency turns repetitive find-and-replace into one-liners that compound across operational tasks.
- In-place editing.
-iedits files directly; supports bulk file modification across many files. - Substitution.
s/pattern/replacement/is the workhorse; the operation that covers most use cases. - Address ranges.
1,5{...}restricts to specific lines; narrows scope without affecting other content. - Pattern space plus scripting. sed maintains state across lines; multi-command scripts support complex transformations.
The approach
The practical approach: dry-run first to stdout, narrow with addresses, use alternative delimiters when paths appear, prefer extended regex. The team’s discipline produces predictable text edits.
- sed 's/old/new/g' file. Global replace, output to stdout; the canonical first call; verifies before in-place edit.
- sed -i.bak 's/old/new/g' file. In-place with backup; produces safe in-place edits with a recoverable original.
- sed -n '5,10p' file. Print lines 5-10; supports targeted extraction without manual counting.
- sed 's|/old/path|/new/path|g'. Alternative delimiter; avoids escape-hell when slashes appear in the pattern.
- sed -E. Extended regex; supports modern patterns; the right default for non-trivial substitutions.
Why this compounds
sed fluency compounds across tasks. Each one-liner captures transformation knowledge; the team’s text-processing muscle grows; new tasks reuse the patterns.
- Faster bulk edits. sed one-liners replace manual editing; the operator runs the command, not the change.
- Reusable patterns. Standard sed invocations become team patterns; the team library compounds across years.
- Universal availability. sed is on every Unix box; no dependencies, no install, always available.
- Institutional knowledge. Each sed command teaches stream processing; the team’s automation muscle grows.
sed fluency is an operational discipline that pays off across years. Nova AI Ops integrates with operations telemetry, surfaces patterns, and supports the team’s automation discipline.