Replace a word with multiple lines using sed?

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

    You can do this with AWK using variable substitution. We can set a variable in AWK using -v, and then use AWK's gsub function to substitute all occurrences of a regular expression with that variable.

    For example, if the file test has the following contents ...

    foo
    bar
    blah _data_and_data_
    foo
    _data_ foobar _data_ again
    

    ... and the Bash variable $DATA is ...

    1
    2
    3
    4
    5
    

    ... then awk -v r=$DATA '{gsub(/_data_/,r)}1' test replaces all occurrences of the regular expression _data_ in the file test with the contents of $DATA, resulting in the following:

    foo
    bar
    blah 1
    2
    3
    4
    5and1
    2
    3
    4
    5
    foo
    1
    2
    3
    4
    5 foobar 1
    2
    3
    4
    5 again
    

提交回复
热议问题