Inserting file into other file after pattern [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • Inserting a file into another file after the 1st occurrence of a pattern

    1 answer



How might I insert the content of the file a, followed by a newline,
into the file b, after the first line containing PATTERN?










share|improve this question













marked as duplicate by don_crissti, RalfFriedl, Thomas, αғsнιη, Goro yesterday


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    0
    down vote

    favorite













    This question already has an answer here:



    • Inserting a file into another file after the 1st occurrence of a pattern

      1 answer



    How might I insert the content of the file a, followed by a newline,
    into the file b, after the first line containing PATTERN?










    share|improve this question













    marked as duplicate by don_crissti, RalfFriedl, Thomas, αғsнιη, Goro yesterday


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:



      • Inserting a file into another file after the 1st occurrence of a pattern

        1 answer



      How might I insert the content of the file a, followed by a newline,
      into the file b, after the first line containing PATTERN?










      share|improve this question














      This question already has an answer here:



      • Inserting a file into another file after the 1st occurrence of a pattern

        1 answer



      How might I insert the content of the file a, followed by a newline,
      into the file b, after the first line containing PATTERN?





      This question already has an answer here:



      • Inserting a file into another file after the 1st occurrence of a pattern

        1 answer







      text-processing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Toothrot

      781517




      781517




      marked as duplicate by don_crissti, RalfFriedl, Thomas, αғsнιη, Goro yesterday


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by don_crissti, RalfFriedl, Thomas, αғsнιη, Goro yesterday


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          sed '/pattern/r file_b
          a
          :f n; b f ' file_a


          If file_b is already terminated by a newline, and you don't want an empty line in the output, omit the a line from the script.



          On the last line, the :f defines a label, n reads a newline (which will be automatically printed since the -n option was not given to sed), and b f branches to the f label, creating a loop. All this in order to append file_b only after the first line matching pattern. If you want to append file_b after each line matching pattern, it's much simpler:



          sed '/pattern/r file_b' file_a


          Example:



          $ cat file_a
          first
          second
          third
          fourth
          first
          second
          third
          fourth
          $ cat file_b
          b1
          b2
          b3

          $ sed '/second/r file_b
          :f n; b f ' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          third
          fourth

          $ printf "xnynz" >file_c
          $ sed '/second/r file_c
          a
          :f n; b f ' file_a
          first
          second
          x
          y
          z
          third
          fourth
          first
          second
          third
          fourth

          $ sed '/second/r file_b' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          b1
          b2
          b3
          third
          fourth


          Another solution would be using ed(1) (which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):



          echo '/pattern/r file_b
          w' | ed file_a


          This will edit file_a in place and append a newline if there's any at the end of file_b; if you want the output written to another file, change the w to w output_file.






          share|improve this answer






















          • Thanks. Could you please explain the last line?
            – Toothrot
            yesterday










          • So the loop is necessary because there is no exit command?
            – Toothrot
            yesterday






          • 1




            No, there is an exit command: q. But if you use it, no line that comes after pattern in file_a will be printed.
            – mosvy
            yesterday

















          up vote
          0
          down vote













          How about



          sed '/PATTERN/r a' b





          share|improve this answer




















          • You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
            – ilkkachu
            yesterday










          • This is a subset of mosvy’s answer. If you believe that there are problems with that other (earlier) answer that your post resolves, explain that.  (Please do not respond in comments; edit your answer to make it clearer and more complete.) Otherwise, this answer would appear to add no new information.
            – G-Man
            yesterday







          • 2




            @G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
            – don_crissti
            yesterday


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          sed '/pattern/r file_b
          a
          :f n; b f ' file_a


          If file_b is already terminated by a newline, and you don't want an empty line in the output, omit the a line from the script.



          On the last line, the :f defines a label, n reads a newline (which will be automatically printed since the -n option was not given to sed), and b f branches to the f label, creating a loop. All this in order to append file_b only after the first line matching pattern. If you want to append file_b after each line matching pattern, it's much simpler:



          sed '/pattern/r file_b' file_a


          Example:



          $ cat file_a
          first
          second
          third
          fourth
          first
          second
          third
          fourth
          $ cat file_b
          b1
          b2
          b3

          $ sed '/second/r file_b
          :f n; b f ' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          third
          fourth

          $ printf "xnynz" >file_c
          $ sed '/second/r file_c
          a
          :f n; b f ' file_a
          first
          second
          x
          y
          z
          third
          fourth
          first
          second
          third
          fourth

          $ sed '/second/r file_b' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          b1
          b2
          b3
          third
          fourth


          Another solution would be using ed(1) (which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):



          echo '/pattern/r file_b
          w' | ed file_a


          This will edit file_a in place and append a newline if there's any at the end of file_b; if you want the output written to another file, change the w to w output_file.






          share|improve this answer






















          • Thanks. Could you please explain the last line?
            – Toothrot
            yesterday










          • So the loop is necessary because there is no exit command?
            – Toothrot
            yesterday






          • 1




            No, there is an exit command: q. But if you use it, no line that comes after pattern in file_a will be printed.
            – mosvy
            yesterday














          up vote
          2
          down vote



          accepted










          sed '/pattern/r file_b
          a
          :f n; b f ' file_a


          If file_b is already terminated by a newline, and you don't want an empty line in the output, omit the a line from the script.



          On the last line, the :f defines a label, n reads a newline (which will be automatically printed since the -n option was not given to sed), and b f branches to the f label, creating a loop. All this in order to append file_b only after the first line matching pattern. If you want to append file_b after each line matching pattern, it's much simpler:



          sed '/pattern/r file_b' file_a


          Example:



          $ cat file_a
          first
          second
          third
          fourth
          first
          second
          third
          fourth
          $ cat file_b
          b1
          b2
          b3

          $ sed '/second/r file_b
          :f n; b f ' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          third
          fourth

          $ printf "xnynz" >file_c
          $ sed '/second/r file_c
          a
          :f n; b f ' file_a
          first
          second
          x
          y
          z
          third
          fourth
          first
          second
          third
          fourth

          $ sed '/second/r file_b' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          b1
          b2
          b3
          third
          fourth


          Another solution would be using ed(1) (which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):



          echo '/pattern/r file_b
          w' | ed file_a


          This will edit file_a in place and append a newline if there's any at the end of file_b; if you want the output written to another file, change the w to w output_file.






          share|improve this answer






















          • Thanks. Could you please explain the last line?
            – Toothrot
            yesterday










          • So the loop is necessary because there is no exit command?
            – Toothrot
            yesterday






          • 1




            No, there is an exit command: q. But if you use it, no line that comes after pattern in file_a will be printed.
            – mosvy
            yesterday












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          sed '/pattern/r file_b
          a
          :f n; b f ' file_a


          If file_b is already terminated by a newline, and you don't want an empty line in the output, omit the a line from the script.



          On the last line, the :f defines a label, n reads a newline (which will be automatically printed since the -n option was not given to sed), and b f branches to the f label, creating a loop. All this in order to append file_b only after the first line matching pattern. If you want to append file_b after each line matching pattern, it's much simpler:



          sed '/pattern/r file_b' file_a


          Example:



          $ cat file_a
          first
          second
          third
          fourth
          first
          second
          third
          fourth
          $ cat file_b
          b1
          b2
          b3

          $ sed '/second/r file_b
          :f n; b f ' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          third
          fourth

          $ printf "xnynz" >file_c
          $ sed '/second/r file_c
          a
          :f n; b f ' file_a
          first
          second
          x
          y
          z
          third
          fourth
          first
          second
          third
          fourth

          $ sed '/second/r file_b' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          b1
          b2
          b3
          third
          fourth


          Another solution would be using ed(1) (which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):



          echo '/pattern/r file_b
          w' | ed file_a


          This will edit file_a in place and append a newline if there's any at the end of file_b; if you want the output written to another file, change the w to w output_file.






          share|improve this answer














          sed '/pattern/r file_b
          a
          :f n; b f ' file_a


          If file_b is already terminated by a newline, and you don't want an empty line in the output, omit the a line from the script.



          On the last line, the :f defines a label, n reads a newline (which will be automatically printed since the -n option was not given to sed), and b f branches to the f label, creating a loop. All this in order to append file_b only after the first line matching pattern. If you want to append file_b after each line matching pattern, it's much simpler:



          sed '/pattern/r file_b' file_a


          Example:



          $ cat file_a
          first
          second
          third
          fourth
          first
          second
          third
          fourth
          $ cat file_b
          b1
          b2
          b3

          $ sed '/second/r file_b
          :f n; b f ' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          third
          fourth

          $ printf "xnynz" >file_c
          $ sed '/second/r file_c
          a
          :f n; b f ' file_a
          first
          second
          x
          y
          z
          third
          fourth
          first
          second
          third
          fourth

          $ sed '/second/r file_b' file_a
          first
          second
          b1
          b2
          b3
          third
          fourth
          first
          second
          b1
          b2
          b3
          third
          fourth


          Another solution would be using ed(1) (which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):



          echo '/pattern/r file_b
          w' | ed file_a


          This will edit file_a in place and append a newline if there's any at the end of file_b; if you want the output written to another file, change the w to w output_file.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          mosvy

          2,002110




          2,002110











          • Thanks. Could you please explain the last line?
            – Toothrot
            yesterday










          • So the loop is necessary because there is no exit command?
            – Toothrot
            yesterday






          • 1




            No, there is an exit command: q. But if you use it, no line that comes after pattern in file_a will be printed.
            – mosvy
            yesterday
















          • Thanks. Could you please explain the last line?
            – Toothrot
            yesterday










          • So the loop is necessary because there is no exit command?
            – Toothrot
            yesterday






          • 1




            No, there is an exit command: q. But if you use it, no line that comes after pattern in file_a will be printed.
            – mosvy
            yesterday















          Thanks. Could you please explain the last line?
          – Toothrot
          yesterday




          Thanks. Could you please explain the last line?
          – Toothrot
          yesterday












          So the loop is necessary because there is no exit command?
          – Toothrot
          yesterday




          So the loop is necessary because there is no exit command?
          – Toothrot
          yesterday




          1




          1




          No, there is an exit command: q. But if you use it, no line that comes after pattern in file_a will be printed.
          – mosvy
          yesterday




          No, there is an exit command: q. But if you use it, no line that comes after pattern in file_a will be printed.
          – mosvy
          yesterday












          up vote
          0
          down vote













          How about



          sed '/PATTERN/r a' b





          share|improve this answer




















          • You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
            – ilkkachu
            yesterday










          • This is a subset of mosvy’s answer. If you believe that there are problems with that other (earlier) answer that your post resolves, explain that.  (Please do not respond in comments; edit your answer to make it clearer and more complete.) Otherwise, this answer would appear to add no new information.
            – G-Man
            yesterday







          • 2




            @G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
            – don_crissti
            yesterday















          up vote
          0
          down vote













          How about



          sed '/PATTERN/r a' b





          share|improve this answer




















          • You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
            – ilkkachu
            yesterday










          • This is a subset of mosvy’s answer. If you believe that there are problems with that other (earlier) answer that your post resolves, explain that.  (Please do not respond in comments; edit your answer to make it clearer and more complete.) Otherwise, this answer would appear to add no new information.
            – G-Man
            yesterday







          • 2




            @G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
            – don_crissti
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          How about



          sed '/PATTERN/r a' b





          share|improve this answer












          How about



          sed '/PATTERN/r a' b






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          RudiC

          1,86719




          1,86719











          • You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
            – ilkkachu
            yesterday










          • This is a subset of mosvy’s answer. If you believe that there are problems with that other (earlier) answer that your post resolves, explain that.  (Please do not respond in comments; edit your answer to make it clearer and more complete.) Otherwise, this answer would appear to add no new information.
            – G-Man
            yesterday







          • 2




            @G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
            – don_crissti
            yesterday

















          • You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
            – ilkkachu
            yesterday










          • This is a subset of mosvy’s answer. If you believe that there are problems with that other (earlier) answer that your post resolves, explain that.  (Please do not respond in comments; edit your answer to make it clearer and more complete.) Otherwise, this answer would appear to add no new information.
            – G-Man
            yesterday







          • 2




            @G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
            – don_crissti
            yesterday
















          You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
          – ilkkachu
          yesterday




          You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
          – ilkkachu
          yesterday












          This is a subset of mosvy’s answer. If you believe that there are problems with that other (earlier) answer that your post resolves, explain that.  (Please do not respond in comments; edit your answer to make it clearer and more complete.) Otherwise, this answer would appear to add no new information.
          – G-Man
          yesterday





          This is a subset of mosvy’s answer. If you believe that there are problems with that other (earlier) answer that your post resolves, explain that.  (Please do not respond in comments; edit your answer to make it clearer and more complete.) Otherwise, this answer would appear to add no new information.
          – G-Man
          yesterday





          2




          2




          @G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
          – don_crissti
          yesterday





          @G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
          – don_crissti
          yesterday



          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?