Cobra_Fast Cobra_Fast - 1 year ago 83
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


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
to replace something with multiple lines of text?

(Alternatives to
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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download