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 e
ring bearer's answer didn't work for me; I think the usage of tr
there is wrong, and the way it's written, it simply strips away newlines by use of echo
.
Instead, I used sed
. I used code from another answer to replace newlines (credit: Zsolt Botykai). I also expected some dollar signs ($
) in my input so I took care of that too. You might need to add other input handling. Note the use of double quotes in echo
to preserve newlines.
DATA="$(cat whatever)"
ESCAPED_DATA="$(echo "${DATA}" | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's/\$/\\$/g')"
Then you can use ${ESCAPED_DATA}
in sed
:
cat input | sed 's/one liner/'"${ESCAPED_DATA}"'/' > output
Just thought I'd share.