sed w and = to Write Lines and Numbers
sed w writes the current line to a named file, and = prints the line number, both useful for routing and locating content.
Beyond editing in place, sed can fan lines out to files with w and report where matches occur with =, turning it into a lightweight splitter and locator.
What it does
The w filename command appends the current pattern space to a file, so an addressed w copies matching lines into a separate file. The = command prints the current line number to stdout, ahead of the line itself unless suppressed with -n.
Common usage
# copy matching lines into a separate file
sed -n '/ERROR/w errors.log' app.log
# print line numbers of matches (grep -n style)
sed -n '/TODO/=' src.txt
# the s command can also write with its w flag
sed -n 's/^WARN/&/w warnings.log' app.logOptions
| Command | What it does |
|---|---|
| w file | Append the pattern space to file |
| /re/w file | Write only matching lines to file |
| = | Print the current input line number |
| s/.../.../w file | Write lines the substitution changed to file |
| -n with = | Print only the numbers, not the lines too |
In CI
w is a tidy way to split a build log into matching and non-matching parts in one pass, and = gives grep -n style line numbers without leaving sed. Both are POSIX and portable, though the file named by w is created relative to the working directory of the step.
Common errors in CI
sed: couldn't open file ...: No such file or directory or Permission denied means the w target path is in a missing or read-only directory; write to a path the runner can create, such as one under the workspace. Output appearing in the wrong order with = usually means -n was omitted, so the line and its number both print.