sed Command Reference for CI Scripts
sed applies edit commands to a stream of text, line by line.
sed is the standard tool for find-and-replace in files during a build. The number-one portability bug is -i, which differs between GNU (Linux) and BSD (macOS) sed.
Common flags/usage
- -i[SUFFIX]: edit in place (GNU accepts -i; BSD needs -i with an empty arg)
- -e SCRIPT: add an editing command
- -E / -r: use extended regular expressions
- -n: suppress auto-print, used with the p command
- s/re/rep/flags: substitute (g for all, I to ignore case)
Example
shell
sed -i "s/0.0.0/${VERSION}/" package.json # GNU sed on Linux runners
sed -n '1,20p' build.log # print lines 1-20
sed -E 's#https?://[^/]+#PLACEHOLDER#g' config.txtIn CI
On Linux runners GNU sed accepts sed -i; BSD/macOS sed needs sed -i '' with an explicit empty suffix. When replacing paths that contain slashes, change the delimiter (s#a#b#) to avoid "unterminated s command". For portable in-place edits, perl -pi -e works everywhere.
Key takeaways
- GNU sed -i edits in place on Linux; BSD/macOS sed needs sed -i with an empty arg.
- Switch the s/// delimiter to # or | when the replacement contains slashes.
- Use -E for extended regex so groups and alternation work without backslashes.
Related guides
awk Command Reference for CI Scriptsawk is a pattern-scanning text processor for CI. Reference for -F field separator, -v variables, and $1/$NF f…
grep Command Reference for CI Scriptsgrep searches text for matching lines in CI. Reference for -r, -E, -i, -q, and -v, plus the exit-code-1-means…
tr Command Reference for CI Scriptstr translates, squeezes, or deletes characters from stdin in CI. Reference for -d, -s, -c, and ranges, plus t…
envsubst Command Reference for CI Scriptsenvsubst substitutes environment variables into templates in CI. Reference for variable lists and the over-su…