Skip to content
Latchkey

sed -e and ; for Multiple Commands

sed can run several commands in a single pass using repeated -e flags, semicolons, or a script file with -f.

One sed invocation can do many edits. Chain them with -e or semicolons so the file is read once instead of piping sed into sed.

What it does

Each -e adds one command to the program, and sed runs them in order on every line. Within a single -e you can also separate commands with semicolons or newlines. A -f file reads the whole program from a script file.

Common usage

Terminal
# multiple -e flags
sed -e 's/foo/bar/' -e 's/baz/qux/' file

# semicolons in one expression
sed 's/foo/bar/; s/baz/qux/; /^#/d' file

# program from a file
sed -f edits.sed input.txt

Options

FormWhat it does
-e cmdAdd a command; repeatable for many edits
cmd1; cmd2Separate commands with a semicolon
newlineA literal newline also separates commands
-f fileRead the sed program from a script file
order mattersEarlier edits feed later ones in the same pass

In CI

Chaining is more efficient than piping multiple sed processes and reads the file once. Note that a, i, and c commands do not mix cleanly after a semicolon on all builds because their text runs to end of line; give those their own -e.

Common errors in CI

sed: -e expression #1, char N: extra characters after command means a command that does not accept a trailing argument was followed by more text without a separator, such as 5d6 instead of 5d;6d. Putting a text-taking command like a before a semicolon swallows the next command as its text.

Related guides

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