How to insert file content after a certain string in a file? [duplicate]

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











up vote
1
down vote

favorite













This question already has an answer here:



  • Replace string with contents of a file using sed

    3 answers



we have for example this file



cat exam.txt

I am expert linux man
what we can do for out country
I love redhat machine
"propertie"
centos less then redhat
fedore what
my name is moon yea


we want to add the content of any file as file.txt after properties line



cat file.txt

324325
5326436
3245235
646346
545
643
6436
63525
664
46454


so I try the following:



a=` cat file `

sed -i '/propertie/a `echo "$a"` ' exam.txt


but not works



any suggestion with sed/awk/perl one liner in order to add content of file after certain string?



expected output



I am expert linux man
what we can do for out country
I love redhat machine
"propertie"
324325
5326436
3245235
646346
545
643
6436
63525
664
46454
centos less then redhat
fedore what
my name is moon yea






share|improve this question













marked as duplicate by αғsнιη, G-Man, Isaac, GAD3R, Kiwy May 29 at 13:31


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
    1
    down vote

    favorite













    This question already has an answer here:



    • Replace string with contents of a file using sed

      3 answers



    we have for example this file



    cat exam.txt

    I am expert linux man
    what we can do for out country
    I love redhat machine
    "propertie"
    centos less then redhat
    fedore what
    my name is moon yea


    we want to add the content of any file as file.txt after properties line



    cat file.txt

    324325
    5326436
    3245235
    646346
    545
    643
    6436
    63525
    664
    46454


    so I try the following:



    a=` cat file `

    sed -i '/propertie/a `echo "$a"` ' exam.txt


    but not works



    any suggestion with sed/awk/perl one liner in order to add content of file after certain string?



    expected output



    I am expert linux man
    what we can do for out country
    I love redhat machine
    "propertie"
    324325
    5326436
    3245235
    646346
    545
    643
    6436
    63525
    664
    46454
    centos less then redhat
    fedore what
    my name is moon yea






    share|improve this question













    marked as duplicate by αғsнιη, G-Man, Isaac, GAD3R, Kiwy May 29 at 13:31


    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
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite












      This question already has an answer here:



      • Replace string with contents of a file using sed

        3 answers



      we have for example this file



      cat exam.txt

      I am expert linux man
      what we can do for out country
      I love redhat machine
      "propertie"
      centos less then redhat
      fedore what
      my name is moon yea


      we want to add the content of any file as file.txt after properties line



      cat file.txt

      324325
      5326436
      3245235
      646346
      545
      643
      6436
      63525
      664
      46454


      so I try the following:



      a=` cat file `

      sed -i '/propertie/a `echo "$a"` ' exam.txt


      but not works



      any suggestion with sed/awk/perl one liner in order to add content of file after certain string?



      expected output



      I am expert linux man
      what we can do for out country
      I love redhat machine
      "propertie"
      324325
      5326436
      3245235
      646346
      545
      643
      6436
      63525
      664
      46454
      centos less then redhat
      fedore what
      my name is moon yea






      share|improve this question














      This question already has an answer here:



      • Replace string with contents of a file using sed

        3 answers



      we have for example this file



      cat exam.txt

      I am expert linux man
      what we can do for out country
      I love redhat machine
      "propertie"
      centos less then redhat
      fedore what
      my name is moon yea


      we want to add the content of any file as file.txt after properties line



      cat file.txt

      324325
      5326436
      3245235
      646346
      545
      643
      6436
      63525
      664
      46454


      so I try the following:



      a=` cat file `

      sed -i '/propertie/a `echo "$a"` ' exam.txt


      but not works



      any suggestion with sed/awk/perl one liner in order to add content of file after certain string?



      expected output



      I am expert linux man
      what we can do for out country
      I love redhat machine
      "propertie"
      324325
      5326436
      3245235
      646346
      545
      643
      6436
      63525
      664
      46454
      centos less then redhat
      fedore what
      my name is moon yea




      This question already has an answer here:



      • Replace string with contents of a file using sed

        3 answers









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 28 at 18:36









      Jeff Schaller

      31k846105




      31k846105









      asked May 28 at 18:10









      yael

      1,9251144




      1,9251144




      marked as duplicate by αғsнιη, G-Man, Isaac, GAD3R, Kiwy May 29 at 13:31


      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 αғsнιη, G-Man, Isaac, GAD3R, Kiwy May 29 at 13:31


      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.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          You almost never want to store the complete contents of a file in a variable in a Unix shell script. If you find yourself doing that, ask yourself whether there's another solution. If you can't find one on your own, come here and we'll look at it :-)




          $ sed '/propertie/r file.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea


          The r ("read") command in sed takes a filename as its argument and inserts the file's content into the current stream.



          If you need the added content indented, then make sure that the content of file.txt is indented before running sed:



          $ sed 's/^/ /' file.txt >file-ind.txt
          $ sed '/propertie/r file-ind.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea



          With ed (calling sed for the indentation of the inserted file). This also does in-place editing of the file and replaces the original with the modified contents.



          ed -s exam.txt <<END_ED
          /propertie/r !sed 's/^/ /' file.txt
          wq
          END_ED


          The r command in ed is able to read the output of an external command if the command is prefixed with !. We use this to indent the data that we'd like to insert. It is otherwise, for obvious reasons, very similar to the sed solution above.



          The only downside with using ed is that you can't generally use it on very large files. sed is for editing streams of undetermined lengths, while ed is ok for editing documents that you could see yourself opening in any other editor, i.e. not files of many megabytes or gigabytes in size.






          share|improve this answer























          • I am also up-vote , great answer and fast answer
            – yael
            May 28 at 19:37










          • @JeffSchaller Look again.
            – Kusalananda
            May 28 at 19:46










          • Upvoted twice! :)
            – Jeff Schaller
            May 28 at 19:49

















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          You almost never want to store the complete contents of a file in a variable in a Unix shell script. If you find yourself doing that, ask yourself whether there's another solution. If you can't find one on your own, come here and we'll look at it :-)




          $ sed '/propertie/r file.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea


          The r ("read") command in sed takes a filename as its argument and inserts the file's content into the current stream.



          If you need the added content indented, then make sure that the content of file.txt is indented before running sed:



          $ sed 's/^/ /' file.txt >file-ind.txt
          $ sed '/propertie/r file-ind.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea



          With ed (calling sed for the indentation of the inserted file). This also does in-place editing of the file and replaces the original with the modified contents.



          ed -s exam.txt <<END_ED
          /propertie/r !sed 's/^/ /' file.txt
          wq
          END_ED


          The r command in ed is able to read the output of an external command if the command is prefixed with !. We use this to indent the data that we'd like to insert. It is otherwise, for obvious reasons, very similar to the sed solution above.



          The only downside with using ed is that you can't generally use it on very large files. sed is for editing streams of undetermined lengths, while ed is ok for editing documents that you could see yourself opening in any other editor, i.e. not files of many megabytes or gigabytes in size.






          share|improve this answer























          • I am also up-vote , great answer and fast answer
            – yael
            May 28 at 19:37










          • @JeffSchaller Look again.
            – Kusalananda
            May 28 at 19:46










          • Upvoted twice! :)
            – Jeff Schaller
            May 28 at 19:49














          up vote
          6
          down vote



          accepted










          You almost never want to store the complete contents of a file in a variable in a Unix shell script. If you find yourself doing that, ask yourself whether there's another solution. If you can't find one on your own, come here and we'll look at it :-)




          $ sed '/propertie/r file.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea


          The r ("read") command in sed takes a filename as its argument and inserts the file's content into the current stream.



          If you need the added content indented, then make sure that the content of file.txt is indented before running sed:



          $ sed 's/^/ /' file.txt >file-ind.txt
          $ sed '/propertie/r file-ind.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea



          With ed (calling sed for the indentation of the inserted file). This also does in-place editing of the file and replaces the original with the modified contents.



          ed -s exam.txt <<END_ED
          /propertie/r !sed 's/^/ /' file.txt
          wq
          END_ED


          The r command in ed is able to read the output of an external command if the command is prefixed with !. We use this to indent the data that we'd like to insert. It is otherwise, for obvious reasons, very similar to the sed solution above.



          The only downside with using ed is that you can't generally use it on very large files. sed is for editing streams of undetermined lengths, while ed is ok for editing documents that you could see yourself opening in any other editor, i.e. not files of many megabytes or gigabytes in size.






          share|improve this answer























          • I am also up-vote , great answer and fast answer
            – yael
            May 28 at 19:37










          • @JeffSchaller Look again.
            – Kusalananda
            May 28 at 19:46










          • Upvoted twice! :)
            – Jeff Schaller
            May 28 at 19:49












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          You almost never want to store the complete contents of a file in a variable in a Unix shell script. If you find yourself doing that, ask yourself whether there's another solution. If you can't find one on your own, come here and we'll look at it :-)




          $ sed '/propertie/r file.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea


          The r ("read") command in sed takes a filename as its argument and inserts the file's content into the current stream.



          If you need the added content indented, then make sure that the content of file.txt is indented before running sed:



          $ sed 's/^/ /' file.txt >file-ind.txt
          $ sed '/propertie/r file-ind.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea



          With ed (calling sed for the indentation of the inserted file). This also does in-place editing of the file and replaces the original with the modified contents.



          ed -s exam.txt <<END_ED
          /propertie/r !sed 's/^/ /' file.txt
          wq
          END_ED


          The r command in ed is able to read the output of an external command if the command is prefixed with !. We use this to indent the data that we'd like to insert. It is otherwise, for obvious reasons, very similar to the sed solution above.



          The only downside with using ed is that you can't generally use it on very large files. sed is for editing streams of undetermined lengths, while ed is ok for editing documents that you could see yourself opening in any other editor, i.e. not files of many megabytes or gigabytes in size.






          share|improve this answer















          You almost never want to store the complete contents of a file in a variable in a Unix shell script. If you find yourself doing that, ask yourself whether there's another solution. If you can't find one on your own, come here and we'll look at it :-)




          $ sed '/propertie/r file.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea


          The r ("read") command in sed takes a filename as its argument and inserts the file's content into the current stream.



          If you need the added content indented, then make sure that the content of file.txt is indented before running sed:



          $ sed 's/^/ /' file.txt >file-ind.txt
          $ sed '/propertie/r file-ind.txt' exam.txt
          I am expert linux man
          what we can do for out country
          I love redhat machine
          "propertie"
          324325
          5326436
          3245235
          646346
          545
          643
          6436
          63525
          664
          46454
          centos less then redhat
          fedore what
          my name is moon yea



          With ed (calling sed for the indentation of the inserted file). This also does in-place editing of the file and replaces the original with the modified contents.



          ed -s exam.txt <<END_ED
          /propertie/r !sed 's/^/ /' file.txt
          wq
          END_ED


          The r command in ed is able to read the output of an external command if the command is prefixed with !. We use this to indent the data that we'd like to insert. It is otherwise, for obvious reasons, very similar to the sed solution above.



          The only downside with using ed is that you can't generally use it on very large files. sed is for editing streams of undetermined lengths, while ed is ok for editing documents that you could see yourself opening in any other editor, i.e. not files of many megabytes or gigabytes in size.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited May 28 at 19:52


























          answered May 28 at 18:14









          Kusalananda

          102k13199314




          102k13199314











          • I am also up-vote , great answer and fast answer
            – yael
            May 28 at 19:37










          • @JeffSchaller Look again.
            – Kusalananda
            May 28 at 19:46










          • Upvoted twice! :)
            – Jeff Schaller
            May 28 at 19:49
















          • I am also up-vote , great answer and fast answer
            – yael
            May 28 at 19:37










          • @JeffSchaller Look again.
            – Kusalananda
            May 28 at 19:46










          • Upvoted twice! :)
            – Jeff Schaller
            May 28 at 19:49















          I am also up-vote , great answer and fast answer
          – yael
          May 28 at 19:37




          I am also up-vote , great answer and fast answer
          – yael
          May 28 at 19:37












          @JeffSchaller Look again.
          – Kusalananda
          May 28 at 19:46




          @JeffSchaller Look again.
          – Kusalananda
          May 28 at 19:46












          Upvoted twice! :)
          – Jeff Schaller
          May 28 at 19:49




          Upvoted twice! :)
          – Jeff Schaller
          May 28 at 19:49


          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?