sed -z: Null-Separated and Multiline Edits
GNU sed -z uses the NUL byte as the line separator, which lets a single pattern span what would otherwise be multiple lines.
Normally sed processes one line at a time, so a regex cannot cross a newline. With -z the whole input becomes one record and newlines are ordinary characters you can match.
What it does
The -z flag makes GNU sed split input on the NUL byte instead of the newline. Because newlines are no longer record separators, a pattern can match across lines, and -z also pairs with find -print0 to handle filenames safely.
Common usage
# replace a newline-spanning block (whole file is one record)
sed -z 's/foo\nbar/replaced/' file
# process NUL-separated filenames from find safely
find . -name '*.tmp' -print0 | sed -z 's|\./||'
# collapse multiple blank lines treating file as one record
sed -z 's/\n\n\n*/\n\n/g' fileOptions
| Flag | What it does |
|---|---|
| -z | Use NUL as the line separator (GNU only) |
| --null-data | Long form of -z |
| \n in pattern | Now matchable since newline is not a separator |
| with find -print0 | Process NUL-terminated paths without word splitting |
| output | NUL-separated unless you re-add newlines |
In CI
-z is a GNU extension and is not available on BSD sed, so it works on ubuntu-latest but fails on macos-latest. It is the cleanest way to do a multi-line substitution in GNU sed and to handle paths that may contain spaces or newlines from find -print0.
Common errors in CI
On macOS, sed -z raises sed: illegal option -- z because BSD sed has no -z. For a multi-line edit on BSD, use the N command to pull lines into the pattern space first, or switch to GNU sed via gsed.