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

后端 未结 7 788
无人及你
无人及你 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 06:46

    you can use awk

    awk 'FNR==NR{ _[++d]=$0;next}
    /BEGIN realm/{
      print
      for(i=1;i<=d;i++){ print _[i] }
      f=1;next
    }
    /END realm/{f=0}!f' realm.xml server.xml > temp && mv temp server.xml
    

    realm.xml is passed to awk as the first file. FNR==NR means getting the records of the first file passed in and store to variable _. awk will process the next file once FNR!=NR. if awk finds /BEGIN realm/, print the BEGIN realm line, then print what is stored in _. By setting a flag (f) to 1, the rest of the lines after BEGIN realm will not be printed until /END realm/ is detected.

提交回复
热议问题