How to copy the rest of lines of a file to another fileHow to print all lines after a match up to the end of the file?compare two files get identical list“Ungrep” - which patterns aren't matchedOutputting common lines from 2 files and uncommon lines from both the files in one output fileReplace lines matching a pattern with lines from another file in orderCompare two files and print only the first word of the lines which don't match along with a stringgnuwin bash If .txt file contains string copy filecopy files and append in another directory?How do I create a new txt file from another file which is a list?counting the matched linesHow to grep the rows with same column in different files and print specific column and add onto the original file?

When an outsider describes family relationships, which point of view are they using?

The preposition for the verb (avenge) - avenge sb/sth (on OR from) sb

How to open new window on center of screen

How can I change the name of a partition?

How do we create new idioms and use them in a novel?

Is there stress on two letters on the word стоят

Can one live in the U.S. and not use a credit card?

When to use a QR code on a business card?

How can I portion out frozen cookie dough?

How to copy the rest of lines of a file to another file

How would an energy-based "projectile" blow up a spaceship?

How do I raise a figure (placed with wrapfig) to be flush with the top of a paragraph?

Did Amazon pay $0 in taxes last year?

Can the Witch Sight warlock invocation see through the Mirror Image spell?

What to do if my university does not offer any advanced math courses?

Insult for someone who "doesn't know anything"

Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

Rationale to prefer local variables over instance variables?

What do you call someone who likes to pick fights?

What would be the most expensive material to an intergalactic society?

Is there a logarithm base for which the logarithm becomes an identity function?

Professor forcing me to attend a conference, I can't afford even with 50% funding

Why is there an extra space when I type "ls" on the Desktop?



How to copy the rest of lines of a file to another file


How to print all lines after a match up to the end of the file?compare two files get identical list“Ungrep” - which patterns aren't matchedOutputting common lines from 2 files and uncommon lines from both the files in one output fileReplace lines matching a pattern with lines from another file in orderCompare two files and print only the first word of the lines which don't match along with a stringgnuwin bash If .txt file contains string copy filecopy files and append in another directory?How do I create a new txt file from another file which is a list?counting the matched linesHow to grep the rows with same column in different files and print specific column and add onto the original file?













1















I have the string xyz which is a line in file1.txt, I want to copy all the lines after xyz in file1.txt to a new file file2.txt. How can I achieve this?



I know about cat command. But how to specify the starting line?










share|improve this question

















  • 1





    Do you want to include that xyz line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz ?

    – don_crissti
    1 hour ago






  • 2





    Possible duplicate of How to print all lines after a match up to the end of the file?

    – Kusalananda
    48 mins ago















1















I have the string xyz which is a line in file1.txt, I want to copy all the lines after xyz in file1.txt to a new file file2.txt. How can I achieve this?



I know about cat command. But how to specify the starting line?










share|improve this question

















  • 1





    Do you want to include that xyz line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz ?

    – don_crissti
    1 hour ago






  • 2





    Possible duplicate of How to print all lines after a match up to the end of the file?

    – Kusalananda
    48 mins ago













1












1








1


0






I have the string xyz which is a line in file1.txt, I want to copy all the lines after xyz in file1.txt to a new file file2.txt. How can I achieve this?



I know about cat command. But how to specify the starting line?










share|improve this question














I have the string xyz which is a line in file1.txt, I want to copy all the lines after xyz in file1.txt to a new file file2.txt. How can I achieve this?



I know about cat command. But how to specify the starting line?







files grep cat file-copy file-transfer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









user9371654user9371654

30117




30117







  • 1





    Do you want to include that xyz line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz ?

    – don_crissti
    1 hour ago






  • 2





    Possible duplicate of How to print all lines after a match up to the end of the file?

    – Kusalananda
    48 mins ago












  • 1





    Do you want to include that xyz line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz ?

    – don_crissti
    1 hour ago






  • 2





    Possible duplicate of How to print all lines after a match up to the end of the file?

    – Kusalananda
    48 mins ago







1




1





Do you want to include that xyz line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz ?

– don_crissti
1 hour ago





Do you want to include that xyz line or exclude it from being copied ? Also, what happens if you have multiple lines matching xyz ?

– don_crissti
1 hour ago




2




2





Possible duplicate of How to print all lines after a match up to the end of the file?

– Kusalananda
48 mins ago





Possible duplicate of How to print all lines after a match up to the end of the file?

– Kusalananda
48 mins ago










4 Answers
4






active

oldest

votes


















2














Using GNU sed



To copy all lines after xyz, try:



sed '0,/xyz/d' file1.txt >file2.txt


1,/xyz/ specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz. d tells sed to delete those lines.



Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt but this only works if the first appearance of xyz is in the second line or later. (Hat tip: kusalananda.)



Example



Consider this test file:



$ cat file1.txt
a
b
xyz
c
d


Run our command:



$ sed '1,/xyz/d' file1.txt >file2.txt
$ cat file2.txt
c
d


Using awk



The same logic can used with awk:



awk 'NR==1,/xyz/next 1' file1.txt >file2.txt


NR==1,/xyz/next tells awk to skip over all lines from the first (NR==1) to the first line matching the regex xyz. 1 tells awk to print any remaining lines.






share|improve this answer




















  • 1





    Note that the sed command will fail if xyz is found on the first line of the file.

    – Kusalananda
    47 mins ago











  • @Kusalananda Thanks. Answer updated to include a GNU sed solution instead.

    – John1024
    39 mins ago











  • The proper way to do this (portably) with sed is shown in mikeserv's answer to the duplicate Q.

    – don_crissti
    36 mins ago












  • @don_crissti Are you referring to this answer? If so, it does not work for me. This is because sed, as currently written, like many other utilities, does not read in a line at a time; it reads in a buffer-full at a time.

    – John1024
    22 mins ago



















0














$ sed -n '/xyz/,$p' file.txt > file2.txt


With -n we prevent sed to print every line. With $ means end of file end p stands for print line. So /xyz/$p means: If a line matches xyz print it until the end of the file.






share|improve this answer























  • This would also print the line matching xyz, not from the line after.

    – Kusalananda
    46 mins ago


















0














There is also csplit :



csplit -s file1.txt %xyz%1





share|improve this answer






























    0














    With ed:



    ed -s file.txt <<< $'/xyz/+1,$w file2.txt'


    This sends one (ranged) command to ed: from the line after (+1) the one containing xyz until the end of the file ($), write those lines to file2.txt.






    share|improve this answer






















      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505388%2fhow-to-copy-the-rest-of-lines-of-a-file-to-another-file%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Using GNU sed



      To copy all lines after xyz, try:



      sed '0,/xyz/d' file1.txt >file2.txt


      1,/xyz/ specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz. d tells sed to delete those lines.



      Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt but this only works if the first appearance of xyz is in the second line or later. (Hat tip: kusalananda.)



      Example



      Consider this test file:



      $ cat file1.txt
      a
      b
      xyz
      c
      d


      Run our command:



      $ sed '1,/xyz/d' file1.txt >file2.txt
      $ cat file2.txt
      c
      d


      Using awk



      The same logic can used with awk:



      awk 'NR==1,/xyz/next 1' file1.txt >file2.txt


      NR==1,/xyz/next tells awk to skip over all lines from the first (NR==1) to the first line matching the regex xyz. 1 tells awk to print any remaining lines.






      share|improve this answer




















      • 1





        Note that the sed command will fail if xyz is found on the first line of the file.

        – Kusalananda
        47 mins ago











      • @Kusalananda Thanks. Answer updated to include a GNU sed solution instead.

        – John1024
        39 mins ago











      • The proper way to do this (portably) with sed is shown in mikeserv's answer to the duplicate Q.

        – don_crissti
        36 mins ago












      • @don_crissti Are you referring to this answer? If so, it does not work for me. This is because sed, as currently written, like many other utilities, does not read in a line at a time; it reads in a buffer-full at a time.

        – John1024
        22 mins ago
















      2














      Using GNU sed



      To copy all lines after xyz, try:



      sed '0,/xyz/d' file1.txt >file2.txt


      1,/xyz/ specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz. d tells sed to delete those lines.



      Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt but this only works if the first appearance of xyz is in the second line or later. (Hat tip: kusalananda.)



      Example



      Consider this test file:



      $ cat file1.txt
      a
      b
      xyz
      c
      d


      Run our command:



      $ sed '1,/xyz/d' file1.txt >file2.txt
      $ cat file2.txt
      c
      d


      Using awk



      The same logic can used with awk:



      awk 'NR==1,/xyz/next 1' file1.txt >file2.txt


      NR==1,/xyz/next tells awk to skip over all lines from the first (NR==1) to the first line matching the regex xyz. 1 tells awk to print any remaining lines.






      share|improve this answer




















      • 1





        Note that the sed command will fail if xyz is found on the first line of the file.

        – Kusalananda
        47 mins ago











      • @Kusalananda Thanks. Answer updated to include a GNU sed solution instead.

        – John1024
        39 mins ago











      • The proper way to do this (portably) with sed is shown in mikeserv's answer to the duplicate Q.

        – don_crissti
        36 mins ago












      • @don_crissti Are you referring to this answer? If so, it does not work for me. This is because sed, as currently written, like many other utilities, does not read in a line at a time; it reads in a buffer-full at a time.

        – John1024
        22 mins ago














      2












      2








      2







      Using GNU sed



      To copy all lines after xyz, try:



      sed '0,/xyz/d' file1.txt >file2.txt


      1,/xyz/ specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz. d tells sed to delete those lines.



      Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt but this only works if the first appearance of xyz is in the second line or later. (Hat tip: kusalananda.)



      Example



      Consider this test file:



      $ cat file1.txt
      a
      b
      xyz
      c
      d


      Run our command:



      $ sed '1,/xyz/d' file1.txt >file2.txt
      $ cat file2.txt
      c
      d


      Using awk



      The same logic can used with awk:



      awk 'NR==1,/xyz/next 1' file1.txt >file2.txt


      NR==1,/xyz/next tells awk to skip over all lines from the first (NR==1) to the first line matching the regex xyz. 1 tells awk to print any remaining lines.






      share|improve this answer















      Using GNU sed



      To copy all lines after xyz, try:



      sed '0,/xyz/d' file1.txt >file2.txt


      1,/xyz/ specifies a range of lines starting with the first and ending with the first occurrence of a line matching xyz. d tells sed to delete those lines.



      Note: For BSD/MacOS sed, one can use sed '1,/xyz/d' file1.txt >file2.txt but this only works if the first appearance of xyz is in the second line or later. (Hat tip: kusalananda.)



      Example



      Consider this test file:



      $ cat file1.txt
      a
      b
      xyz
      c
      d


      Run our command:



      $ sed '1,/xyz/d' file1.txt >file2.txt
      $ cat file2.txt
      c
      d


      Using awk



      The same logic can used with awk:



      awk 'NR==1,/xyz/next 1' file1.txt >file2.txt


      NR==1,/xyz/next tells awk to skip over all lines from the first (NR==1) to the first line matching the regex xyz. 1 tells awk to print any remaining lines.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 40 mins ago

























      answered 1 hour ago









      John1024John1024

      47.5k5110125




      47.5k5110125







      • 1





        Note that the sed command will fail if xyz is found on the first line of the file.

        – Kusalananda
        47 mins ago











      • @Kusalananda Thanks. Answer updated to include a GNU sed solution instead.

        – John1024
        39 mins ago











      • The proper way to do this (portably) with sed is shown in mikeserv's answer to the duplicate Q.

        – don_crissti
        36 mins ago












      • @don_crissti Are you referring to this answer? If so, it does not work for me. This is because sed, as currently written, like many other utilities, does not read in a line at a time; it reads in a buffer-full at a time.

        – John1024
        22 mins ago













      • 1





        Note that the sed command will fail if xyz is found on the first line of the file.

        – Kusalananda
        47 mins ago











      • @Kusalananda Thanks. Answer updated to include a GNU sed solution instead.

        – John1024
        39 mins ago











      • The proper way to do this (portably) with sed is shown in mikeserv's answer to the duplicate Q.

        – don_crissti
        36 mins ago












      • @don_crissti Are you referring to this answer? If so, it does not work for me. This is because sed, as currently written, like many other utilities, does not read in a line at a time; it reads in a buffer-full at a time.

        – John1024
        22 mins ago








      1




      1





      Note that the sed command will fail if xyz is found on the first line of the file.

      – Kusalananda
      47 mins ago





      Note that the sed command will fail if xyz is found on the first line of the file.

      – Kusalananda
      47 mins ago













      @Kusalananda Thanks. Answer updated to include a GNU sed solution instead.

      – John1024
      39 mins ago





      @Kusalananda Thanks. Answer updated to include a GNU sed solution instead.

      – John1024
      39 mins ago













      The proper way to do this (portably) with sed is shown in mikeserv's answer to the duplicate Q.

      – don_crissti
      36 mins ago






      The proper way to do this (portably) with sed is shown in mikeserv's answer to the duplicate Q.

      – don_crissti
      36 mins ago














      @don_crissti Are you referring to this answer? If so, it does not work for me. This is because sed, as currently written, like many other utilities, does not read in a line at a time; it reads in a buffer-full at a time.

      – John1024
      22 mins ago






      @don_crissti Are you referring to this answer? If so, it does not work for me. This is because sed, as currently written, like many other utilities, does not read in a line at a time; it reads in a buffer-full at a time.

      – John1024
      22 mins ago














      0














      $ sed -n '/xyz/,$p' file.txt > file2.txt


      With -n we prevent sed to print every line. With $ means end of file end p stands for print line. So /xyz/$p means: If a line matches xyz print it until the end of the file.






      share|improve this answer























      • This would also print the line matching xyz, not from the line after.

        – Kusalananda
        46 mins ago















      0














      $ sed -n '/xyz/,$p' file.txt > file2.txt


      With -n we prevent sed to print every line. With $ means end of file end p stands for print line. So /xyz/$p means: If a line matches xyz print it until the end of the file.






      share|improve this answer























      • This would also print the line matching xyz, not from the line after.

        – Kusalananda
        46 mins ago













      0












      0








      0







      $ sed -n '/xyz/,$p' file.txt > file2.txt


      With -n we prevent sed to print every line. With $ means end of file end p stands for print line. So /xyz/$p means: If a line matches xyz print it until the end of the file.






      share|improve this answer













      $ sed -n '/xyz/,$p' file.txt > file2.txt


      With -n we prevent sed to print every line. With $ means end of file end p stands for print line. So /xyz/$p means: If a line matches xyz print it until the end of the file.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 1 hour ago









      finswimmerfinswimmer

      72917




      72917












      • This would also print the line matching xyz, not from the line after.

        – Kusalananda
        46 mins ago

















      • This would also print the line matching xyz, not from the line after.

        – Kusalananda
        46 mins ago
















      This would also print the line matching xyz, not from the line after.

      – Kusalananda
      46 mins ago





      This would also print the line matching xyz, not from the line after.

      – Kusalananda
      46 mins ago











      0














      There is also csplit :



      csplit -s file1.txt %xyz%1





      share|improve this answer



























        0














        There is also csplit :



        csplit -s file1.txt %xyz%1





        share|improve this answer

























          0












          0








          0







          There is also csplit :



          csplit -s file1.txt %xyz%1





          share|improve this answer













          There is also csplit :



          csplit -s file1.txt %xyz%1






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 19 mins ago









          ctac_ctac_

          1,4221210




          1,4221210





















              0














              With ed:



              ed -s file.txt <<< $'/xyz/+1,$w file2.txt'


              This sends one (ranged) command to ed: from the line after (+1) the one containing xyz until the end of the file ($), write those lines to file2.txt.






              share|improve this answer



























                0














                With ed:



                ed -s file.txt <<< $'/xyz/+1,$w file2.txt'


                This sends one (ranged) command to ed: from the line after (+1) the one containing xyz until the end of the file ($), write those lines to file2.txt.






                share|improve this answer

























                  0












                  0








                  0







                  With ed:



                  ed -s file.txt <<< $'/xyz/+1,$w file2.txt'


                  This sends one (ranged) command to ed: from the line after (+1) the one containing xyz until the end of the file ($), write those lines to file2.txt.






                  share|improve this answer













                  With ed:



                  ed -s file.txt <<< $'/xyz/+1,$w file2.txt'


                  This sends one (ranged) command to ed: from the line after (+1) the one containing xyz until the end of the file ($), write those lines to file2.txt.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 10 mins ago









                  Jeff SchallerJeff Schaller

                  43.2k1159138




                  43.2k1159138



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505388%2fhow-to-copy-the-rest-of-lines-of-a-file-to-another-file%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Isabella Eugénie Boyer Biographie | Références | Menu de navigationmodifiermodifier le codeComparator to Compute the Relative Value of a U.S. Dollar Amount – 1774 to Present.

                      Join wedge with single bond in chemfigHow to make only one part of double bond bold with chemfig?Crossing bonds in chemfigjoining atoms in chemfig. Two adjacent molculesHow do I selectively change bond length in chemfig?Ugly bond joints in chemfigchemfig: reaction above arrowUsing the mhchem and chemfig packages in conjunctionBonding to specific element letter using chemfigResonance hybrids in chemfigScale chemfig molecule in beamer with tikzWhy does this chemfig bond with a hook start in the middle of the atom?

                      Are small insurances worth itIs insurance worth it if you can afford to replace the item? If not, when is it?Is accident insurance worth it for my kids who play sportsIs insuring property for more than it is worth allowed?At what point does it become worth it to file an insurance claim?Are wage loss insurance programs worth the cost compared to having an emergency fund?When is an event worth insuring against?Is insurance worth it if you can afford to replace the item? If not, when is it?FHA loan just commenced : Any way to get any of the up-front mortgage insurance back?Which types of insurances do I need to buy?Should I carry less renter's insurance if I can self-insure?Mortgage Adviser Signed Me Up For Multiple Home and Life Insurances (UK)Why many travel insurances don't cover country of nationality?