sed c: Replace Whole Lines by Address
sed c deletes the addressed line or range and emits replacement text in its place.
When the new value is easier to write than a substitution pattern, c replaces the whole line. On a range it replaces the entire block with a single copy of the text.
What it does
The c command changes lines. Applied to a single address it swaps that one line for the given text. Applied to a range, it deletes every line in the range and prints the replacement text once, after the range ends.
Common usage
# replace a single matching line
sed '/^LogLevel/c LogLevel info' app.conf
# replace an entire block with one line
sed '/^# BEGIN/,/^# END/c # block removed' config.txt
# replace line 1 outright
sed '1c #!/usr/bin/env bash' script.shOptions
| Form | What it does |
|---|---|
| /regex/c text | Replace each matching line with text |
| Nc text | Replace line number N with text |
| M,Nc text | Replace the whole range with one copy of text |
| c\ then newline | POSIX/BSD portable change syntax |
| multi-line text | End continuation lines with a trailing backslash |
In CI
c on a range emits the replacement only once, which is what you want for collapsing a generated block down to a marker. Like a and i, the one-line c text form is GNU-friendly; use the backslash-newline form for macOS runners.
Common errors in CI
On BSD sed, sed '1c text' file gives sed: 1: "1c text": command c expects \ followed by text. If a range c unexpectedly drops content, remember it deletes the whole range, not just the first line, before printing the replacement.