Replace a word with multiple lines using sed?

前端 未结 9 1669
礼貌的吻别
礼貌的吻别 2020-11-27 06:32

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

9条回答
  •  失恋的感觉
    2020-11-27 07:16

    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.

提交回复
热议问题