I have a configuration script which does basically this:
#!/bin/bash
source vars.sh
cat >foo.output <<EOF
# some static stuff (many lines...)
variable_bit=($SOME_VAR, $SOME_OTHER_VAR, ...)
# some more static stuff (many lines...)
EOF
It would be nicer to have some other file, call it foo.output.template
, which looked like this
# some static stuff (many lines...)
VARIABLE_BIT
# some more static stuff (many lines...)
Then, my configuration script could do something like this
sed 's/VARIABLE_BIT/<<STDIN>>/' foo.output.template >foo.output <<EOF
variable_bit=($SOME_VAR, $SOME_OTHER_VAR, ...)
EOF
This won't work, and I don't know if sed
can do this, but the intention is that it
- reads foo.output.template
- matches
VARIABLE_BIT
- replaces this with whatever is in stdin
- then pipes to foo.output
Can sed
, awk
, or some other common *nix utility do this easily?
(read -r line; sed "s/VARIABLE_BIT/$line/" ... ) << ...
– Mark Plotnick Jun 19 '18 at 20:47cat foo | sed -e '/VARIABLE_BIT/{r /dev/stdin' -e 'd;}' foo.output.template > foo.output
– steeldriver Jun 19 '18 at 22:16