To summarize the various options for passing a script (program) [made up of multiple commands] to sed:
Let's assume you want to perform the following 2 commands:
on input 'ab', so as to get 'AB'.
Use a single-line string and separate the commands with ;:
echo 'ab' | sed 's/a/A/; s/b/B/'
Caveat: Some sed implementations - notably, FreeBSD/macOS sed require actual line breaks as part of certain commands, such as for defining labels and branching to them; typically, using separate -e options (see below) is an alternative.
For a comprehensive summary of the differences between GNU and FreeBSD/macOS sed, see this answer.
Use a multi-line string and use actual line breaks to separate the commands:
echo 'ab' | sed -e '
s/a/A/
s/b/B/
'
Note: You can also use a here-document with the -f option - see below.
Use multiple -e options to pass the commands individually.
echo 'ab' | sed -e 's/a/A/' -e 's/b/B/'
If your option arguments are long and you want to spread the -e options across several lines, terminate all but the last line of the overall command with \ so as to signal to the shell that the command continues on the next line - note that the \ must be the very last character on the line (not even spaces or tabs are allowed after it):
echo 'ab' | sed -e 's/a/A/' \
-e 's/b/B/'
Note that this is a general requirement of the shell and has nothing to do with sed itself.
Write your sed script to a file or use a here-document, and pass it to sed with the -f option:
# Create script file.
cat > myScript <<'EOF'
s/a/A/
s/b/B/
EOF
# Pass to `sed` with `-f`:
echo 'ab' | sed -f myScript
# Alternative, with a here-document via stdin, which requires
# the input to come from an actual file:
# Note: GNU sed also accepts `-f -`, but macOS sed does not.
echo 'ab' > test.txt
sed -f /dev/stdin test.txt <<'EOF'
s/a/A/
s/b/B/
EOF
Note:
- All POSIX-compatible
sed implementations should support all these methods.
You can even mix the -f and -e approaches, where the -e supplied command are appended to the overall script; e.g.: echo 'abc' | sed -f myScript -e 's/c/C/' # -> 'ABC'
If you use the variant with the here-document as the script (<<'EOF'...), use -f /dev/stdin to be portable (only GNU sed supports -f -).