how to print every fifth row in a file












1















I have a file with numbers



20
18
21
16
14
30
40
24


and I need to output four files with rows printed with intervals of 4
So we have rows 1,5,9...



20
14


Then rows 2,6,10...



18
30


Then 3,7,11...



21
40


and then 4,8,12...



16 
24


I did try the code below but it does not give me the control over the starting row



awk 'NR % 4 == 0'









share|improve this question





























    1















    I have a file with numbers



    20
    18
    21
    16
    14
    30
    40
    24


    and I need to output four files with rows printed with intervals of 4
    So we have rows 1,5,9...



    20
    14


    Then rows 2,6,10...



    18
    30


    Then 3,7,11...



    21
    40


    and then 4,8,12...



    16 
    24


    I did try the code below but it does not give me the control over the starting row



    awk 'NR % 4 == 0'









    share|improve this question



























      1












      1








      1








      I have a file with numbers



      20
      18
      21
      16
      14
      30
      40
      24


      and I need to output four files with rows printed with intervals of 4
      So we have rows 1,5,9...



      20
      14


      Then rows 2,6,10...



      18
      30


      Then 3,7,11...



      21
      40


      and then 4,8,12...



      16 
      24


      I did try the code below but it does not give me the control over the starting row



      awk 'NR % 4 == 0'









      share|improve this question
















      I have a file with numbers



      20
      18
      21
      16
      14
      30
      40
      24


      and I need to output four files with rows printed with intervals of 4
      So we have rows 1,5,9...



      20
      14


      Then rows 2,6,10...



      18
      30


      Then 3,7,11...



      21
      40


      and then 4,8,12...



      16 
      24


      I did try the code below but it does not give me the control over the starting row



      awk 'NR % 4 == 0'






      bash awk sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 17:34









      kvantour

      9,66831731




      9,66831731










      asked Nov 14 '18 at 17:05









      vkaul11vkaul11

      1,40343053




      1,40343053
























          4 Answers
          4






          active

          oldest

          votes


















          3














          In a single awk you can do:



          awk '{print > ("file" (NR%4))}' inputfile


          This will send the output to files file0, file1, file2 and file3






          share|improve this answer































            1














            You may use these awk commands:



            awk -v n=1 'NR%4 == n%4' file
            20
            14

            awk -v n=2 'NR%4 == n%4' file
            18
            30

            awk -v n=3 'NR%4 == n%4' file
            21
            40

            awk -v n=4 'NR%4 == n%4' file
            16
            24





            share|improve this answer































              0














              IMHO awk is the best solution. You can use sed:

              Inputfile generated with seq 12:



              for ((i=1;i<5; i++)); do 
              sed -n $i~4w$i.out <(seq 12)
              done


              Here w$i.out writes to file $i.out.






              share|improve this answer































                0














                This might work for you (GNU sed):



                sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  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: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  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%2fstackoverflow.com%2fquestions%2f53305380%2fhow-to-print-every-fifth-row-in-a-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









                  3














                  In a single awk you can do:



                  awk '{print > ("file" (NR%4))}' inputfile


                  This will send the output to files file0, file1, file2 and file3






                  share|improve this answer




























                    3














                    In a single awk you can do:



                    awk '{print > ("file" (NR%4))}' inputfile


                    This will send the output to files file0, file1, file2 and file3






                    share|improve this answer


























                      3












                      3








                      3







                      In a single awk you can do:



                      awk '{print > ("file" (NR%4))}' inputfile


                      This will send the output to files file0, file1, file2 and file3






                      share|improve this answer













                      In a single awk you can do:



                      awk '{print > ("file" (NR%4))}' inputfile


                      This will send the output to files file0, file1, file2 and file3







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 14 '18 at 17:29









                      kvantourkvantour

                      9,66831731




                      9,66831731

























                          1














                          You may use these awk commands:



                          awk -v n=1 'NR%4 == n%4' file
                          20
                          14

                          awk -v n=2 'NR%4 == n%4' file
                          18
                          30

                          awk -v n=3 'NR%4 == n%4' file
                          21
                          40

                          awk -v n=4 'NR%4 == n%4' file
                          16
                          24





                          share|improve this answer




























                            1














                            You may use these awk commands:



                            awk -v n=1 'NR%4 == n%4' file
                            20
                            14

                            awk -v n=2 'NR%4 == n%4' file
                            18
                            30

                            awk -v n=3 'NR%4 == n%4' file
                            21
                            40

                            awk -v n=4 'NR%4 == n%4' file
                            16
                            24





                            share|improve this answer


























                              1












                              1








                              1







                              You may use these awk commands:



                              awk -v n=1 'NR%4 == n%4' file
                              20
                              14

                              awk -v n=2 'NR%4 == n%4' file
                              18
                              30

                              awk -v n=3 'NR%4 == n%4' file
                              21
                              40

                              awk -v n=4 'NR%4 == n%4' file
                              16
                              24





                              share|improve this answer













                              You may use these awk commands:



                              awk -v n=1 'NR%4 == n%4' file
                              20
                              14

                              awk -v n=2 'NR%4 == n%4' file
                              18
                              30

                              awk -v n=3 'NR%4 == n%4' file
                              21
                              40

                              awk -v n=4 'NR%4 == n%4' file
                              16
                              24






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 14 '18 at 17:11









                              anubhavaanubhava

                              532k47331408




                              532k47331408























                                  0














                                  IMHO awk is the best solution. You can use sed:

                                  Inputfile generated with seq 12:



                                  for ((i=1;i<5; i++)); do 
                                  sed -n $i~4w$i.out <(seq 12)
                                  done


                                  Here w$i.out writes to file $i.out.






                                  share|improve this answer




























                                    0














                                    IMHO awk is the best solution. You can use sed:

                                    Inputfile generated with seq 12:



                                    for ((i=1;i<5; i++)); do 
                                    sed -n $i~4w$i.out <(seq 12)
                                    done


                                    Here w$i.out writes to file $i.out.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      IMHO awk is the best solution. You can use sed:

                                      Inputfile generated with seq 12:



                                      for ((i=1;i<5; i++)); do 
                                      sed -n $i~4w$i.out <(seq 12)
                                      done


                                      Here w$i.out writes to file $i.out.






                                      share|improve this answer













                                      IMHO awk is the best solution. You can use sed:

                                      Inputfile generated with seq 12:



                                      for ((i=1;i<5; i++)); do 
                                      sed -n $i~4w$i.out <(seq 12)
                                      done


                                      Here w$i.out writes to file $i.out.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 14 '18 at 20:50









                                      Walter AWalter A

                                      11k21132




                                      11k21132























                                          0














                                          This might work for you (GNU sed):



                                          sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file





                                          share|improve this answer




























                                            0














                                            This might work for you (GNU sed):



                                            sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              This might work for you (GNU sed):



                                              sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file





                                              share|improve this answer













                                              This might work for you (GNU sed):



                                              sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 16 '18 at 1:48









                                              potongpotong

                                              36.2k43062




                                              36.2k43062






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • 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%2fstackoverflow.com%2fquestions%2f53305380%2fhow-to-print-every-fifth-row-in-a-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

                                                  Xamarin.iOS Cant Deploy on Iphone

                                                  Glorious Revolution

                                                  Dulmage-Mendelsohn matrix decomposition in Python