Skip to content
Latchkey

sed s/// Substitute: Replace Text in CI

sed s/pattern/replacement/ replaces the first match of a pattern on every line of input.

Substitution is the command you reach for most. Get the s/old/new/ shape right and the flags later add scope and control.

What it does

sed reads input line by line and the s command swaps text that matches a regular expression. By default it replaces only the first match on each line and prints every line, changed or not.

Common usage

Terminal
echo "color: red" | sed 's/red/blue/'
sed 's/localhost/db.internal/' config.ini
# only print lines that changed
sed -n 's/version=/v=/p' app.properties

Options

PartWhat it does
s/old/new/Replace first match of old with new on each line
patternA basic regular expression (BRE) by default
replacementLiteral text, with & meaning the whole match
-n with p flagPrint only the lines the substitution touched
-E / -rSwitch the pattern to extended regex (ERE)

In CI

On ubuntu-latest sed is GNU sed, which is the most permissive. BSD sed on macOS runners parses the same s/// syntax but differs on flags like in-place editing, so test scripts on the runner OS you actually use.

Common errors in CI

sed: -e expression #1, char N: unterminated `s' command means a missing closing delimiter, usually a slash eaten by a forgotten escape or a variable that contained a slash. sed: -e expression #1, char 0: no previous regular expression means an empty pattern // with nothing to reuse.

Related guides

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