How to replace a multi line string in a bunch files

前端 未结 2 1355
别跟我提以往
别跟我提以往 2021-01-06 14:54
#!/bin/sh
old=\"hello\"
new=\"world\"
sed -i s/\"${old}\"/\"${new}\"/g $(grep \"${old}\" -rl *)

The preceding script just work for single line text

2条回答
  •  走了就别回头了
    2021-01-06 15:28

    awk can do that for you.

    awk 'BEGIN { RS="" }
      FILENAME==ARGV[1] { s=$0 }
      FILENAME==ARGV[2] { r=$0 }
      FILENAME==ARGV[3] { sub(s,r) ; print }
    ' FILE_WITH_CONTENTS_OF_OLD FILE_WITH_CONTENTS_OF_NEW ORIGINALFILE > NEWFILE
    

    But you can do it with vim like described here (scriptable solution).

    Also see this and this in the sed faq.

提交回复
热议问题