Using a here-doc for `sed` and a file
You can tell GNU sed to read the script from standard input with -f -
, -f
meaning to read the script from a file, and -
meaning standard input as is common with a lot of commands.
sed -f - "$IN" > "$OUT" << SED_SCRIPT
s/a/1/g
s/test/full/g
SED_SCRIPT
POSIX sed also supports -f
, but the use of -
for standard input is not documented. In this case, you could use /dev/stdin
on Linux systems (and I seem to recall Solaris has this too, but I cannot confirm that right now)
Using <<-SED_SCRIPT
(with the '-' prefix) will allow the closing SED_SCRIPT
tag to be indented.
In case sed
does not support reading of a script from stdin
(using -f -
), you can use process substitution (available in bash
, zsh
, ksh93
):
sed "$IN" > "$OUT" -f <( cat << SED_SCRIPT
s/a/1/g
s/test/full/g
SED_SCRIPT)
The closing parenthesis )
must follow the end delimiter (SEC_SCRIPT
) immediately or after a newline. In the case of process substitution you can also use echo
instead of a here document:
sed "$IN" > "$OUT" -f <( echo \
" s/a/1/g
s/test/full/g" )