Skip to content
Latchkey

sed: Usage, Options & Common CI Errors

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 the -i flag, which differs between GNU (Linux) and BSD (macOS) sed.

What it does

sed reads input line by line, applies one or more editing commands (most commonly substitution s/find/replace/), and writes the result to stdout - or back to the file with -i.

Common usage

Terminal
sed 's/old/new/g' file.txt              # print with replacements
sed -i 's/1.0.0/${VERSION}/' version.txt  # GNU: edit in place
sed -i '' 's/old/new/' file.txt          # BSD/macOS: -i needs an arg
sed -n '10,20p' file.txt                 # print lines 10-20
sed -E 's/(foo|bar)/x/g' file.txt        # extended regex

Options

FlagWhat it does
-i[SUFFIX]Edit in place (GNU: -i; BSD: -i '')
-e <script>Add an editing command
-E / -rUse extended regular expressions
-nSuppress auto-print (use with p)
s/re/rep/flagsSubstitute; g = all, I = ignore case

Common errors in CI

sed: -i may not be used without an argument / "command a expects \ followed by text" - that is BSD sed (macOS); GNU sed (Linux runners) accepts sed -i, BSD needs sed -i ''. "unterminated s command" means an unescaped delimiter - switch delimiters (s|a|b|) when replacing paths with slashes. "command c expects \ …" differs across implementations; for portable in-place edits prefer perl -pi -e or detect the OS.

Related guides

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