How can I can insert the contents of a file into another file right before a specific line

前端 未结 5 538
囚心锁ツ
囚心锁ツ 2020-12-05 07:46

How can I can insert the contents of a file into another file right before a specific line using sed?

example I have file1.xml that has the following:



        
5条回答
  •  清歌不尽
    2020-12-05 08:33

    Here were the solutions that worked for me:

    1. Using a marker like explained in another reply:

      sed '/StandardMessageTrailer/i MARKER' file1.xml | sed -e '/MARKER/r file2.xml' -e '/MARKER/d'
      
    2. Counting the line when it occured like explained in another reply to a similar question:

      LINE_NUMBER_MATCHING=$(sed -n '/StandardMessageTrailer/=' file1.xml) && sed "$((${LINE_NUMBER_MATCHING} - 1))r file2.xml" file1.xml
      
    3. Or using sed like explained in another reply to a similar question:

      sed $'/StandardMessageTrailer/{e cat file2.xml\n}' file1.xml
      

提交回复
热议问题