Replace a word with multiple lines using sed?

前端 未结 9 1683
礼貌的吻别
礼貌的吻别 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:01

    I tried it and sed 's/pattern/\na\nb\nc/g' but it does not work on all systems. What does work is putting a \ followed by a newline in the replace pattern, like this:

    sed 's/pattern/a\
    b\
    c/g'
    

    This appends a line containing b and a line containing c when the pattern is seen.

    To put it in a variable, use double backslashes:

    export DATA="\\
    a\\
    b\\
    c"
    

    and then:

    sed "s/pattern/${DATA}/g"
    

    Note the double quotes.

提交回复
热议问题