Skip to content
Latchkey

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

Terminal
# 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.sh

Options

FormWhat it does
/regex/c textReplace each matching line with text
Nc textReplace line number N with text
M,Nc textReplace the whole range with one copy of text
c\ then newlinePOSIX/BSD portable change syntax
multi-line textEnd 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.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →