how can i move lines in mulitple text files?

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











up vote
0
down vote

favorite












Hi assume I have a files that look like:



x
x
x
A
B
C
1
2
3
D
E
F
x
x
x


and what need the files to look like:



x
x
x
A
B
C
D
E
F
1
2
3
x
x
x


I know I could do this with a few sed -i s/search/replace/g lines but am wondering if there's a better, easier way to move x number of rows by y numbers in either direction?










share|improve this question



























    up vote
    0
    down vote

    favorite












    Hi assume I have a files that look like:



    x
    x
    x
    A
    B
    C
    1
    2
    3
    D
    E
    F
    x
    x
    x


    and what need the files to look like:



    x
    x
    x
    A
    B
    C
    D
    E
    F
    1
    2
    3
    x
    x
    x


    I know I could do this with a few sed -i s/search/replace/g lines but am wondering if there's a better, easier way to move x number of rows by y numbers in either direction?










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Hi assume I have a files that look like:



      x
      x
      x
      A
      B
      C
      1
      2
      3
      D
      E
      F
      x
      x
      x


      and what need the files to look like:



      x
      x
      x
      A
      B
      C
      D
      E
      F
      1
      2
      3
      x
      x
      x


      I know I could do this with a few sed -i s/search/replace/g lines but am wondering if there's a better, easier way to move x number of rows by y numbers in either direction?










      share|improve this question















      Hi assume I have a files that look like:



      x
      x
      x
      A
      B
      C
      1
      2
      3
      D
      E
      F
      x
      x
      x


      and what need the files to look like:



      x
      x
      x
      A
      B
      C
      D
      E
      F
      1
      2
      3
      x
      x
      x


      I know I could do this with a few sed -i s/search/replace/g lines but am wondering if there's a better, easier way to move x number of rows by y numbers in either direction?







      text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 37 mins ago









      Kusalananda

      110k14213337




      110k14213337










      asked 1 hour ago









      cerr

      66382239




      66382239




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          ed



          echo -e '7,9m12n,p' | ed -s file.txt





          share|improve this answer
















          • 1




            this solution works only under bash, it doesn't work under other environments
            – TNT
            28 mins ago

















          up vote
          6
          down vote













          Using sed (moving rows down, because that's easiest with stream editors):



          $ sed '7,9H;d;; 12G;s/n//;' <file
          x
          x
          x
          A
          B
          C
          D
          E
          F
          1
          2
          3
          x
          x
          x


          You may read the sed script as "move lines 7 through to 9 to after line 12".



          The script annotated:



          7,9 # These are the lines we'd like to move
          H; # Append them to the hold space (separated by newlines)
          d; # Delete from pattern space, start next cycle

          12 # This is where we'd like to move those lines
          G; # Append the hold space
          s/n//; # Remove the newline that the above command inserted

          # (implicit print)


          Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):



          sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file


          I.e., "move the lines from the one starting with 1 through to the one starting with 3 to after the line that starts with F".




          A direct translation of the above into awk:



          awk '/^1/,/^3/ hold = hold ORS $0; next 
          /^F/ $0 = $0 hold
          print ' <file


          Using "line numbers" (record numbers in awk):



          awk 'NR == 7, NR == 9 hold = hold ORS $0; next 
          NR == 12 $0 = $0 hold
          print ' <file


          The condition NR == 7, NR == 9 could also be written NR >= 7 && NR <= 9.



          To be 100% faithful to the sed code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0) but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.






          share|improve this answer


















          • 1




            robust solution thumb up!
            – TNT
            28 mins ago










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f475682%2fhow-can-i-move-lines-in-mulitple-text-files%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
          0
          down vote



          accepted










          ed



          echo -e '7,9m12n,p' | ed -s file.txt





          share|improve this answer
















          • 1




            this solution works only under bash, it doesn't work under other environments
            – TNT
            28 mins ago














          up vote
          0
          down vote



          accepted










          ed



          echo -e '7,9m12n,p' | ed -s file.txt





          share|improve this answer
















          • 1




            this solution works only under bash, it doesn't work under other environments
            – TNT
            28 mins ago












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          ed



          echo -e '7,9m12n,p' | ed -s file.txt





          share|improve this answer












          ed



          echo -e '7,9m12n,p' | ed -s file.txt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 51 mins ago









          ColinSeligSmith

          24415




          24415







          • 1




            this solution works only under bash, it doesn't work under other environments
            – TNT
            28 mins ago












          • 1




            this solution works only under bash, it doesn't work under other environments
            – TNT
            28 mins ago







          1




          1




          this solution works only under bash, it doesn't work under other environments
          – TNT
          28 mins ago




          this solution works only under bash, it doesn't work under other environments
          – TNT
          28 mins ago












          up vote
          6
          down vote













          Using sed (moving rows down, because that's easiest with stream editors):



          $ sed '7,9H;d;; 12G;s/n//;' <file
          x
          x
          x
          A
          B
          C
          D
          E
          F
          1
          2
          3
          x
          x
          x


          You may read the sed script as "move lines 7 through to 9 to after line 12".



          The script annotated:



          7,9 # These are the lines we'd like to move
          H; # Append them to the hold space (separated by newlines)
          d; # Delete from pattern space, start next cycle

          12 # This is where we'd like to move those lines
          G; # Append the hold space
          s/n//; # Remove the newline that the above command inserted

          # (implicit print)


          Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):



          sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file


          I.e., "move the lines from the one starting with 1 through to the one starting with 3 to after the line that starts with F".




          A direct translation of the above into awk:



          awk '/^1/,/^3/ hold = hold ORS $0; next 
          /^F/ $0 = $0 hold
          print ' <file


          Using "line numbers" (record numbers in awk):



          awk 'NR == 7, NR == 9 hold = hold ORS $0; next 
          NR == 12 $0 = $0 hold
          print ' <file


          The condition NR == 7, NR == 9 could also be written NR >= 7 && NR <= 9.



          To be 100% faithful to the sed code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0) but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.






          share|improve this answer


















          • 1




            robust solution thumb up!
            – TNT
            28 mins ago














          up vote
          6
          down vote













          Using sed (moving rows down, because that's easiest with stream editors):



          $ sed '7,9H;d;; 12G;s/n//;' <file
          x
          x
          x
          A
          B
          C
          D
          E
          F
          1
          2
          3
          x
          x
          x


          You may read the sed script as "move lines 7 through to 9 to after line 12".



          The script annotated:



          7,9 # These are the lines we'd like to move
          H; # Append them to the hold space (separated by newlines)
          d; # Delete from pattern space, start next cycle

          12 # This is where we'd like to move those lines
          G; # Append the hold space
          s/n//; # Remove the newline that the above command inserted

          # (implicit print)


          Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):



          sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file


          I.e., "move the lines from the one starting with 1 through to the one starting with 3 to after the line that starts with F".




          A direct translation of the above into awk:



          awk '/^1/,/^3/ hold = hold ORS $0; next 
          /^F/ $0 = $0 hold
          print ' <file


          Using "line numbers" (record numbers in awk):



          awk 'NR == 7, NR == 9 hold = hold ORS $0; next 
          NR == 12 $0 = $0 hold
          print ' <file


          The condition NR == 7, NR == 9 could also be written NR >= 7 && NR <= 9.



          To be 100% faithful to the sed code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0) but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.






          share|improve this answer


















          • 1




            robust solution thumb up!
            – TNT
            28 mins ago












          up vote
          6
          down vote










          up vote
          6
          down vote









          Using sed (moving rows down, because that's easiest with stream editors):



          $ sed '7,9H;d;; 12G;s/n//;' <file
          x
          x
          x
          A
          B
          C
          D
          E
          F
          1
          2
          3
          x
          x
          x


          You may read the sed script as "move lines 7 through to 9 to after line 12".



          The script annotated:



          7,9 # These are the lines we'd like to move
          H; # Append them to the hold space (separated by newlines)
          d; # Delete from pattern space, start next cycle

          12 # This is where we'd like to move those lines
          G; # Append the hold space
          s/n//; # Remove the newline that the above command inserted

          # (implicit print)


          Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):



          sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file


          I.e., "move the lines from the one starting with 1 through to the one starting with 3 to after the line that starts with F".




          A direct translation of the above into awk:



          awk '/^1/,/^3/ hold = hold ORS $0; next 
          /^F/ $0 = $0 hold
          print ' <file


          Using "line numbers" (record numbers in awk):



          awk 'NR == 7, NR == 9 hold = hold ORS $0; next 
          NR == 12 $0 = $0 hold
          print ' <file


          The condition NR == 7, NR == 9 could also be written NR >= 7 && NR <= 9.



          To be 100% faithful to the sed code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0) but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.






          share|improve this answer














          Using sed (moving rows down, because that's easiest with stream editors):



          $ sed '7,9H;d;; 12G;s/n//;' <file
          x
          x
          x
          A
          B
          C
          D
          E
          F
          1
          2
          3
          x
          x
          x


          You may read the sed script as "move lines 7 through to 9 to after line 12".



          The script annotated:



          7,9 # These are the lines we'd like to move
          H; # Append them to the hold space (separated by newlines)
          d; # Delete from pattern space, start next cycle

          12 # This is where we'd like to move those lines
          G; # Append the hold space
          s/n//; # Remove the newline that the above command inserted

          # (implicit print)


          Since lines may be addressed by regular expressions (not just line numbers), the following would work too (on this particular data):



          sed '/^1/,/^3/H;d;; /^F/G;s/n//;' <file


          I.e., "move the lines from the one starting with 1 through to the one starting with 3 to after the line that starts with F".




          A direct translation of the above into awk:



          awk '/^1/,/^3/ hold = hold ORS $0; next 
          /^F/ $0 = $0 hold
          print ' <file


          Using "line numbers" (record numbers in awk):



          awk 'NR == 7, NR == 9 hold = hold ORS $0; next 
          NR == 12 $0 = $0 hold
          print ' <file


          The condition NR == 7, NR == 9 could also be written NR >= 7 && NR <= 9.



          To be 100% faithful to the sed code, the second block should read $0 = $0 ORS hold; sub(ORS, "", $0) but it's a bit silly to insert the output record separator (a newline by default) only to remove it immediately afterwards.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 16 mins ago

























          answered 39 mins ago









          Kusalananda

          110k14213337




          110k14213337







          • 1




            robust solution thumb up!
            – TNT
            28 mins ago












          • 1




            robust solution thumb up!
            – TNT
            28 mins ago







          1




          1




          robust solution thumb up!
          – TNT
          28 mins ago




          robust solution thumb up!
          – TNT
          28 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475682%2fhow-can-i-move-lines-in-mulitple-text-files%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          Installing NextGIS Connect into QGIS 3?

          One-line joke