Skip to content
Latchkey

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.txt

In 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →