Cut specific lines of a file and paste them at the end of another file

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








up vote
0
down vote

favorite












Suppose I want to cut more than one interval of lines from one file (eg., lines 1-500, 1029-1729 and 2696-3446), appending values at the end of another file (output.txt) and eliminating these values from the first file. The origin is a file with 9277 lines, I want to cut some of them, eliminating them from the original file and pasting them into another file. Is that possible via command line?







share|improve this question


















  • 3




    Example input and output, please.
    – muru
    May 29 at 8:41










  • Is my improvement in the problem description enough?
    – Anselmo Ferreira
    May 29 at 8:51










  • unix.stackexchange.com/questions/47407/…
    – M. Becerra
    May 29 at 9:02














up vote
0
down vote

favorite












Suppose I want to cut more than one interval of lines from one file (eg., lines 1-500, 1029-1729 and 2696-3446), appending values at the end of another file (output.txt) and eliminating these values from the first file. The origin is a file with 9277 lines, I want to cut some of them, eliminating them from the original file and pasting them into another file. Is that possible via command line?







share|improve this question


















  • 3




    Example input and output, please.
    – muru
    May 29 at 8:41










  • Is my improvement in the problem description enough?
    – Anselmo Ferreira
    May 29 at 8:51










  • unix.stackexchange.com/questions/47407/…
    – M. Becerra
    May 29 at 9:02












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Suppose I want to cut more than one interval of lines from one file (eg., lines 1-500, 1029-1729 and 2696-3446), appending values at the end of another file (output.txt) and eliminating these values from the first file. The origin is a file with 9277 lines, I want to cut some of them, eliminating them from the original file and pasting them into another file. Is that possible via command line?







share|improve this question














Suppose I want to cut more than one interval of lines from one file (eg., lines 1-500, 1029-1729 and 2696-3446), appending values at the end of another file (output.txt) and eliminating these values from the first file. The origin is a file with 9277 lines, I want to cut some of them, eliminating them from the original file and pasting them into another file. Is that possible via command line?









share|improve this question













share|improve this question




share|improve this question








edited May 29 at 8:49

























asked May 29 at 8:34









Anselmo Ferreira

1285




1285







  • 3




    Example input and output, please.
    – muru
    May 29 at 8:41










  • Is my improvement in the problem description enough?
    – Anselmo Ferreira
    May 29 at 8:51










  • unix.stackexchange.com/questions/47407/…
    – M. Becerra
    May 29 at 9:02












  • 3




    Example input and output, please.
    – muru
    May 29 at 8:41










  • Is my improvement in the problem description enough?
    – Anselmo Ferreira
    May 29 at 8:51










  • unix.stackexchange.com/questions/47407/…
    – M. Becerra
    May 29 at 9:02







3




3




Example input and output, please.
– muru
May 29 at 8:41




Example input and output, please.
– muru
May 29 at 8:41












Is my improvement in the problem description enough?
– Anselmo Ferreira
May 29 at 8:51




Is my improvement in the problem description enough?
– Anselmo Ferreira
May 29 at 8:51












unix.stackexchange.com/questions/47407/…
– M. Becerra
May 29 at 9:02




unix.stackexchange.com/questions/47407/…
– M. Becerra
May 29 at 9:02










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










Using sed, you can write a set of lines to a different file while deleting it from the current file like so:





sed -i -e 'N, M w output.txt
d ' input.txt


where N and M are the line numbers. The -i option makes sed save changes to the source file, and here the d command deletes to those lines. At the same time, w output.txt causes the selected lines to be written to output.txt. And yes, those are two separate lines: sed requires that the w command's filename be till a newline.



So you can do something like:



cmd=' w output.txt
d '
sed -i -e "1,500 $cmd" -e "1029,1729 $cmd" -e "2696,3446 $cmd" input.txt





share|improve this answer



























    up vote
    1
    down vote













    possible !



    1. copy lines to dest.file with redirected outout of grep 'pattern' src.file >> dest.file


    2. delete lines from src.file with sed -i '/pattern/d' src.file






    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "89"
      ;
      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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1041487%2fcut-specific-lines-of-a-file-and-paste-them-at-the-end-of-another-file%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      Using sed, you can write a set of lines to a different file while deleting it from the current file like so:





      sed -i -e 'N, M w output.txt
      d ' input.txt


      where N and M are the line numbers. The -i option makes sed save changes to the source file, and here the d command deletes to those lines. At the same time, w output.txt causes the selected lines to be written to output.txt. And yes, those are two separate lines: sed requires that the w command's filename be till a newline.



      So you can do something like:



      cmd=' w output.txt
      d '
      sed -i -e "1,500 $cmd" -e "1029,1729 $cmd" -e "2696,3446 $cmd" input.txt





      share|improve this answer
























        up vote
        3
        down vote



        accepted










        Using sed, you can write a set of lines to a different file while deleting it from the current file like so:





        sed -i -e 'N, M w output.txt
        d ' input.txt


        where N and M are the line numbers. The -i option makes sed save changes to the source file, and here the d command deletes to those lines. At the same time, w output.txt causes the selected lines to be written to output.txt. And yes, those are two separate lines: sed requires that the w command's filename be till a newline.



        So you can do something like:



        cmd=' w output.txt
        d '
        sed -i -e "1,500 $cmd" -e "1029,1729 $cmd" -e "2696,3446 $cmd" input.txt





        share|improve this answer






















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          Using sed, you can write a set of lines to a different file while deleting it from the current file like so:





          sed -i -e 'N, M w output.txt
          d ' input.txt


          where N and M are the line numbers. The -i option makes sed save changes to the source file, and here the d command deletes to those lines. At the same time, w output.txt causes the selected lines to be written to output.txt. And yes, those are two separate lines: sed requires that the w command's filename be till a newline.



          So you can do something like:



          cmd=' w output.txt
          d '
          sed -i -e "1,500 $cmd" -e "1029,1729 $cmd" -e "2696,3446 $cmd" input.txt





          share|improve this answer












          Using sed, you can write a set of lines to a different file while deleting it from the current file like so:





          sed -i -e 'N, M w output.txt
          d ' input.txt


          where N and M are the line numbers. The -i option makes sed save changes to the source file, and here the d command deletes to those lines. At the same time, w output.txt causes the selected lines to be written to output.txt. And yes, those are two separate lines: sed requires that the w command's filename be till a newline.



          So you can do something like:



          cmd=' w output.txt
          d '
          sed -i -e "1,500 $cmd" -e "1029,1729 $cmd" -e "2696,3446 $cmd" input.txt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 29 at 9:20









          muru

          128k19270460




          128k19270460






















              up vote
              1
              down vote













              possible !



              1. copy lines to dest.file with redirected outout of grep 'pattern' src.file >> dest.file


              2. delete lines from src.file with sed -i '/pattern/d' src.file






              share|improve this answer
























                up vote
                1
                down vote













                possible !



                1. copy lines to dest.file with redirected outout of grep 'pattern' src.file >> dest.file


                2. delete lines from src.file with sed -i '/pattern/d' src.file






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  possible !



                  1. copy lines to dest.file with redirected outout of grep 'pattern' src.file >> dest.file


                  2. delete lines from src.file with sed -i '/pattern/d' src.file






                  share|improve this answer












                  possible !



                  1. copy lines to dest.file with redirected outout of grep 'pattern' src.file >> dest.file


                  2. delete lines from src.file with sed -i '/pattern/d' src.file







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 29 at 9:04









                  cmak.fr

                  1,529918




                  1,529918






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1041487%2fcut-specific-lines-of-a-file-and-paste-them-at-the-end-of-another-file%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      pylint3 and pip3 broken

                      Missing snmpget and snmpwalk

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491