Replace delimited block of text in file with the contents of another file

后端 未结 7 791
无人及你
无人及你 2020-12-09 06:32

I need to write a simple script to replace a block of text in a configuration file with the contents of another file.

Let\'s assume with have the following simplifie

7条回答
  •  情歌与酒
    2020-12-09 07:00

    I was unable to get Dennis solution easily working on OS X (its BSD sed is slightly different). I found this other solution that I was able to make work on both Linux and OS X (I have a mixed environment). The original version on superuser.com works only on Linux, here I fixed it:

    lead='^$'
    tail='^'
    sed  -e '/'"$lead"'/,/'"$tail"'/{ /'"$lead"'/{p; r realm.xml' -e' }; /'"$tail"'/p; d;} '  server.xml
    

    Here a version of Dennis code that works also on OS X (using multiple lines):

    sed -ne '/'"$lead"'/ {
     p
     r realm.xml
     :a
     n 
     /'"$tail"'/ {
      p
      b
     } 
     ba
     }
    p' server.xml
    

    Both these codes print the output on stdout. Use redirection or, to substitute the file inline, add the option '-i' (on linux) or '-i ""' (on BSD/OS X).

提交回复
热议问题