Cobra_Fast Cobra_Fast - 1 year ago 55
Bash Question

Replace a word with multiple lines using sed?

I'm working on a bash-script that has to prepare an E-Mail for being sent to a user.

It aggregates some data, which ends up being multiple lines of stuff. For the example stored in

$DATA
.

Now, after a bit of stfw I found a few things like
sed -ei "s/_data_/${DATA}/g" mail.tpl
and also sed replace with variable with multiple lines. None of them work.

Now the question is, how do I get
sed
to replace something with multiple lines of text?

(Alternatives to
sed
are also welcome!)

Answer Source

If you build your multiple line text with "\n"s, this will work with a simple sed command as:

DATA=`echo ${DATA} | tr '\n' "\\n"`
#now, DATA="line1\nline2\nline3"
sed "s/_data_/${DATA}/" mail.tpl