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
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.propertiesOptions
| Part | What it does |
|---|---|
| s/old/new/ | Replace first match of old with new on each line |
| pattern | A basic regular expression (BRE) by default |
| replacement | Literal text, with & meaning the whole match |
| -n with p flag | Print only the lines the substitution touched |
| -E / -r | Switch 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.