Reading first N lines from each member of a zip archive

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











up vote
0
down vote

favorite












There are 10 files and they are zipped into Ten.zip. How to read first n lines (say 2 lines) from all 10 files in a zipped file? Is there any easy command for this?







share|improve this question






















  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 11 at 13:41














up vote
0
down vote

favorite












There are 10 files and they are zipped into Ten.zip. How to read first n lines (say 2 lines) from all 10 files in a zipped file? Is there any easy command for this?







share|improve this question






















  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 11 at 13:41












up vote
0
down vote

favorite









up vote
0
down vote

favorite











There are 10 files and they are zipped into Ten.zip. How to read first n lines (say 2 lines) from all 10 files in a zipped file? Is there any easy command for this?







share|improve this question














There are 10 files and they are zipped into Ten.zip. How to read first n lines (say 2 lines) from all 10 files in a zipped file? Is there any easy command for this?









share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 16:41









Jeff Schaller

31.3k846105




31.3k846105










asked Feb 7 at 7:34









Radhakrishnan Rk

475




475











  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 11 at 13:41
















  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Feb 11 at 13:41















If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 11 at 13:41




If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Feb 11 at 13:41










3 Answers
3






active

oldest

votes

















up vote
1
down vote













There is not an "easy" command because unzip does not offer this functionality natively. You must ask it to extract some or all of the files and process them yourself. Here's another way:



zipinfo -1 Ten.zip | while IFS= read -r filename
do
unzip -p Ten.zip "$filename" | sed 2q
done


The difference here is to use zipinfo to list the archive's contents, one per line; we then read those filenames line by line and ask unzip to extract that file to the screen (with -p so that the filename is not printed) and then pipe that through sed to have it print (by default), quitting at line 2.



This has a chance to fail if you had archived files with newlines in their name; you wouldn't do that, though, because unzip mangles the filename upon extraction:



$ touch file$'n'name
$ zip foo.zip file*name
$ rm file*name
$ zipinfo -1 foo.zip
file^Jname
$ unzip foo.zip
Archive: foo.zip
extracting: filename
$ ls -lrt
...
filename





share|improve this answer



























    up vote
    0
    down vote













    Using unzip and grep:



    (Note: This answer takes reference of @RomanPerekhrest's answer and a post here)



    bash-3.2$ unzip -l ten.zip
    Archive: ten.zip
    Length Date Time Name
    --------- ---------- ----- ----
    6 02-07-2018 09:16 0.txt
    6 02-07-2018 09:16 1.txt
    6 02-07-2018 09:16 2.txt
    6 02-07-2018 09:16 3.txt
    6 02-07-2018 09:16 4.txt
    6 02-07-2018 09:16 5.txt
    6 02-07-2018 09:16 6.txt
    6 02-07-2018 09:16 7.txt
    6 02-07-2018 09:16 8.txt
    6 02-07-2018 09:16 9.txt
    --------- -------
    60 10 files
    bash-3.2$


    Extract the contents of zipped files to stdout/screen:



    bash-3.2$ unzip -c ten.zip | grep -A2 extracting
    extracting: 0.txt
    0
    0
    --
    extracting: 1.txt
    1
    1
    --
    extracting: 2.txt
    2
    2
    --
    extracting: 3.txt
    3
    3
    And so on..


    Command: unzip -c ten.zip | grep -A[n] extracting.
    Here 'n' can be the number of lines user wants to view.






    share|improve this answer




















    • Problematic if the word extracting actually appears in the files, and extracting may not be the operation displayed (experimenting on one system I see " inflating:" instead).
      – XrXca
      Feb 8 at 18:40










    • I agree @XrXca. The answer is always subject to change to meet the user's requirements.
      – GC 13
      Feb 8 at 18:43

















    up vote
    0
    down vote













    unzip + awk solution:



    Sample 10xml.zip archive structure:



    $ unzip -l 10xml.zip 
    Archive: 10xml.zip
    Length Date Time Name
    --------- ---------- ----- ----
    20 2018-02-07 11:20 home/osboxes/temp_files/1.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/2.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/3.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/4.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/5.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/6.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/7.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/8.xml
    20 2018-02-07 11:20 home/osboxes/temp_files/9.xml
    21 2018-02-07 11:20 home/osboxes/temp_files/10.xml
    --------- -------
    201 10 files




    • unzip -l <archive> - list archive files (short format)


    Extracting the first 2 lines from each file in the archive:



    unzip -c 10xml.zip | awk -v n=2 'NR==1 next /^ +inflating:/ n=NR+2; next n && NR<=n'



    • unzip -c <archive> - extract files to stdout/screen. The name of each file is printed as it is extracted.





    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',
      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%2f422472%2freading-first-n-lines-from-each-member-of-a-zip-archive%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      There is not an "easy" command because unzip does not offer this functionality natively. You must ask it to extract some or all of the files and process them yourself. Here's another way:



      zipinfo -1 Ten.zip | while IFS= read -r filename
      do
      unzip -p Ten.zip "$filename" | sed 2q
      done


      The difference here is to use zipinfo to list the archive's contents, one per line; we then read those filenames line by line and ask unzip to extract that file to the screen (with -p so that the filename is not printed) and then pipe that through sed to have it print (by default), quitting at line 2.



      This has a chance to fail if you had archived files with newlines in their name; you wouldn't do that, though, because unzip mangles the filename upon extraction:



      $ touch file$'n'name
      $ zip foo.zip file*name
      $ rm file*name
      $ zipinfo -1 foo.zip
      file^Jname
      $ unzip foo.zip
      Archive: foo.zip
      extracting: filename
      $ ls -lrt
      ...
      filename





      share|improve this answer
























        up vote
        1
        down vote













        There is not an "easy" command because unzip does not offer this functionality natively. You must ask it to extract some or all of the files and process them yourself. Here's another way:



        zipinfo -1 Ten.zip | while IFS= read -r filename
        do
        unzip -p Ten.zip "$filename" | sed 2q
        done


        The difference here is to use zipinfo to list the archive's contents, one per line; we then read those filenames line by line and ask unzip to extract that file to the screen (with -p so that the filename is not printed) and then pipe that through sed to have it print (by default), quitting at line 2.



        This has a chance to fail if you had archived files with newlines in their name; you wouldn't do that, though, because unzip mangles the filename upon extraction:



        $ touch file$'n'name
        $ zip foo.zip file*name
        $ rm file*name
        $ zipinfo -1 foo.zip
        file^Jname
        $ unzip foo.zip
        Archive: foo.zip
        extracting: filename
        $ ls -lrt
        ...
        filename





        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          There is not an "easy" command because unzip does not offer this functionality natively. You must ask it to extract some or all of the files and process them yourself. Here's another way:



          zipinfo -1 Ten.zip | while IFS= read -r filename
          do
          unzip -p Ten.zip "$filename" | sed 2q
          done


          The difference here is to use zipinfo to list the archive's contents, one per line; we then read those filenames line by line and ask unzip to extract that file to the screen (with -p so that the filename is not printed) and then pipe that through sed to have it print (by default), quitting at line 2.



          This has a chance to fail if you had archived files with newlines in their name; you wouldn't do that, though, because unzip mangles the filename upon extraction:



          $ touch file$'n'name
          $ zip foo.zip file*name
          $ rm file*name
          $ zipinfo -1 foo.zip
          file^Jname
          $ unzip foo.zip
          Archive: foo.zip
          extracting: filename
          $ ls -lrt
          ...
          filename





          share|improve this answer












          There is not an "easy" command because unzip does not offer this functionality natively. You must ask it to extract some or all of the files and process them yourself. Here's another way:



          zipinfo -1 Ten.zip | while IFS= read -r filename
          do
          unzip -p Ten.zip "$filename" | sed 2q
          done


          The difference here is to use zipinfo to list the archive's contents, one per line; we then read those filenames line by line and ask unzip to extract that file to the screen (with -p so that the filename is not printed) and then pipe that through sed to have it print (by default), quitting at line 2.



          This has a chance to fail if you had archived files with newlines in their name; you wouldn't do that, though, because unzip mangles the filename upon extraction:



          $ touch file$'n'name
          $ zip foo.zip file*name
          $ rm file*name
          $ zipinfo -1 foo.zip
          file^Jname
          $ unzip foo.zip
          Archive: foo.zip
          extracting: filename
          $ ls -lrt
          ...
          filename






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 7 at 16:56









          Jeff Schaller

          31.3k846105




          31.3k846105






















              up vote
              0
              down vote













              Using unzip and grep:



              (Note: This answer takes reference of @RomanPerekhrest's answer and a post here)



              bash-3.2$ unzip -l ten.zip
              Archive: ten.zip
              Length Date Time Name
              --------- ---------- ----- ----
              6 02-07-2018 09:16 0.txt
              6 02-07-2018 09:16 1.txt
              6 02-07-2018 09:16 2.txt
              6 02-07-2018 09:16 3.txt
              6 02-07-2018 09:16 4.txt
              6 02-07-2018 09:16 5.txt
              6 02-07-2018 09:16 6.txt
              6 02-07-2018 09:16 7.txt
              6 02-07-2018 09:16 8.txt
              6 02-07-2018 09:16 9.txt
              --------- -------
              60 10 files
              bash-3.2$


              Extract the contents of zipped files to stdout/screen:



              bash-3.2$ unzip -c ten.zip | grep -A2 extracting
              extracting: 0.txt
              0
              0
              --
              extracting: 1.txt
              1
              1
              --
              extracting: 2.txt
              2
              2
              --
              extracting: 3.txt
              3
              3
              And so on..


              Command: unzip -c ten.zip | grep -A[n] extracting.
              Here 'n' can be the number of lines user wants to view.






              share|improve this answer




















              • Problematic if the word extracting actually appears in the files, and extracting may not be the operation displayed (experimenting on one system I see " inflating:" instead).
                – XrXca
                Feb 8 at 18:40










              • I agree @XrXca. The answer is always subject to change to meet the user's requirements.
                – GC 13
                Feb 8 at 18:43














              up vote
              0
              down vote













              Using unzip and grep:



              (Note: This answer takes reference of @RomanPerekhrest's answer and a post here)



              bash-3.2$ unzip -l ten.zip
              Archive: ten.zip
              Length Date Time Name
              --------- ---------- ----- ----
              6 02-07-2018 09:16 0.txt
              6 02-07-2018 09:16 1.txt
              6 02-07-2018 09:16 2.txt
              6 02-07-2018 09:16 3.txt
              6 02-07-2018 09:16 4.txt
              6 02-07-2018 09:16 5.txt
              6 02-07-2018 09:16 6.txt
              6 02-07-2018 09:16 7.txt
              6 02-07-2018 09:16 8.txt
              6 02-07-2018 09:16 9.txt
              --------- -------
              60 10 files
              bash-3.2$


              Extract the contents of zipped files to stdout/screen:



              bash-3.2$ unzip -c ten.zip | grep -A2 extracting
              extracting: 0.txt
              0
              0
              --
              extracting: 1.txt
              1
              1
              --
              extracting: 2.txt
              2
              2
              --
              extracting: 3.txt
              3
              3
              And so on..


              Command: unzip -c ten.zip | grep -A[n] extracting.
              Here 'n' can be the number of lines user wants to view.






              share|improve this answer




















              • Problematic if the word extracting actually appears in the files, and extracting may not be the operation displayed (experimenting on one system I see " inflating:" instead).
                – XrXca
                Feb 8 at 18:40










              • I agree @XrXca. The answer is always subject to change to meet the user's requirements.
                – GC 13
                Feb 8 at 18:43












              up vote
              0
              down vote










              up vote
              0
              down vote









              Using unzip and grep:



              (Note: This answer takes reference of @RomanPerekhrest's answer and a post here)



              bash-3.2$ unzip -l ten.zip
              Archive: ten.zip
              Length Date Time Name
              --------- ---------- ----- ----
              6 02-07-2018 09:16 0.txt
              6 02-07-2018 09:16 1.txt
              6 02-07-2018 09:16 2.txt
              6 02-07-2018 09:16 3.txt
              6 02-07-2018 09:16 4.txt
              6 02-07-2018 09:16 5.txt
              6 02-07-2018 09:16 6.txt
              6 02-07-2018 09:16 7.txt
              6 02-07-2018 09:16 8.txt
              6 02-07-2018 09:16 9.txt
              --------- -------
              60 10 files
              bash-3.2$


              Extract the contents of zipped files to stdout/screen:



              bash-3.2$ unzip -c ten.zip | grep -A2 extracting
              extracting: 0.txt
              0
              0
              --
              extracting: 1.txt
              1
              1
              --
              extracting: 2.txt
              2
              2
              --
              extracting: 3.txt
              3
              3
              And so on..


              Command: unzip -c ten.zip | grep -A[n] extracting.
              Here 'n' can be the number of lines user wants to view.






              share|improve this answer












              Using unzip and grep:



              (Note: This answer takes reference of @RomanPerekhrest's answer and a post here)



              bash-3.2$ unzip -l ten.zip
              Archive: ten.zip
              Length Date Time Name
              --------- ---------- ----- ----
              6 02-07-2018 09:16 0.txt
              6 02-07-2018 09:16 1.txt
              6 02-07-2018 09:16 2.txt
              6 02-07-2018 09:16 3.txt
              6 02-07-2018 09:16 4.txt
              6 02-07-2018 09:16 5.txt
              6 02-07-2018 09:16 6.txt
              6 02-07-2018 09:16 7.txt
              6 02-07-2018 09:16 8.txt
              6 02-07-2018 09:16 9.txt
              --------- -------
              60 10 files
              bash-3.2$


              Extract the contents of zipped files to stdout/screen:



              bash-3.2$ unzip -c ten.zip | grep -A2 extracting
              extracting: 0.txt
              0
              0
              --
              extracting: 1.txt
              1
              1
              --
              extracting: 2.txt
              2
              2
              --
              extracting: 3.txt
              3
              3
              And so on..


              Command: unzip -c ten.zip | grep -A[n] extracting.
              Here 'n' can be the number of lines user wants to view.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 7 at 15:28









              GC 13

              419212




              419212











              • Problematic if the word extracting actually appears in the files, and extracting may not be the operation displayed (experimenting on one system I see " inflating:" instead).
                – XrXca
                Feb 8 at 18:40










              • I agree @XrXca. The answer is always subject to change to meet the user's requirements.
                – GC 13
                Feb 8 at 18:43
















              • Problematic if the word extracting actually appears in the files, and extracting may not be the operation displayed (experimenting on one system I see " inflating:" instead).
                – XrXca
                Feb 8 at 18:40










              • I agree @XrXca. The answer is always subject to change to meet the user's requirements.
                – GC 13
                Feb 8 at 18:43















              Problematic if the word extracting actually appears in the files, and extracting may not be the operation displayed (experimenting on one system I see " inflating:" instead).
              – XrXca
              Feb 8 at 18:40




              Problematic if the word extracting actually appears in the files, and extracting may not be the operation displayed (experimenting on one system I see " inflating:" instead).
              – XrXca
              Feb 8 at 18:40












              I agree @XrXca. The answer is always subject to change to meet the user's requirements.
              – GC 13
              Feb 8 at 18:43




              I agree @XrXca. The answer is always subject to change to meet the user's requirements.
              – GC 13
              Feb 8 at 18:43










              up vote
              0
              down vote













              unzip + awk solution:



              Sample 10xml.zip archive structure:



              $ unzip -l 10xml.zip 
              Archive: 10xml.zip
              Length Date Time Name
              --------- ---------- ----- ----
              20 2018-02-07 11:20 home/osboxes/temp_files/1.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/2.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/3.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/4.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/5.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/6.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/7.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/8.xml
              20 2018-02-07 11:20 home/osboxes/temp_files/9.xml
              21 2018-02-07 11:20 home/osboxes/temp_files/10.xml
              --------- -------
              201 10 files




              • unzip -l <archive> - list archive files (short format)


              Extracting the first 2 lines from each file in the archive:



              unzip -c 10xml.zip | awk -v n=2 'NR==1 next /^ +inflating:/ n=NR+2; next n && NR<=n'



              • unzip -c <archive> - extract files to stdout/screen. The name of each file is printed as it is extracted.





              share|improve this answer


























                up vote
                0
                down vote













                unzip + awk solution:



                Sample 10xml.zip archive structure:



                $ unzip -l 10xml.zip 
                Archive: 10xml.zip
                Length Date Time Name
                --------- ---------- ----- ----
                20 2018-02-07 11:20 home/osboxes/temp_files/1.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/2.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/3.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/4.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/5.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/6.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/7.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/8.xml
                20 2018-02-07 11:20 home/osboxes/temp_files/9.xml
                21 2018-02-07 11:20 home/osboxes/temp_files/10.xml
                --------- -------
                201 10 files




                • unzip -l <archive> - list archive files (short format)


                Extracting the first 2 lines from each file in the archive:



                unzip -c 10xml.zip | awk -v n=2 'NR==1 next /^ +inflating:/ n=NR+2; next n && NR<=n'



                • unzip -c <archive> - extract files to stdout/screen. The name of each file is printed as it is extracted.





                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  unzip + awk solution:



                  Sample 10xml.zip archive structure:



                  $ unzip -l 10xml.zip 
                  Archive: 10xml.zip
                  Length Date Time Name
                  --------- ---------- ----- ----
                  20 2018-02-07 11:20 home/osboxes/temp_files/1.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/2.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/3.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/4.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/5.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/6.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/7.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/8.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/9.xml
                  21 2018-02-07 11:20 home/osboxes/temp_files/10.xml
                  --------- -------
                  201 10 files




                  • unzip -l <archive> - list archive files (short format)


                  Extracting the first 2 lines from each file in the archive:



                  unzip -c 10xml.zip | awk -v n=2 'NR==1 next /^ +inflating:/ n=NR+2; next n && NR<=n'



                  • unzip -c <archive> - extract files to stdout/screen. The name of each file is printed as it is extracted.





                  share|improve this answer














                  unzip + awk solution:



                  Sample 10xml.zip archive structure:



                  $ unzip -l 10xml.zip 
                  Archive: 10xml.zip
                  Length Date Time Name
                  --------- ---------- ----- ----
                  20 2018-02-07 11:20 home/osboxes/temp_files/1.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/2.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/3.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/4.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/5.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/6.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/7.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/8.xml
                  20 2018-02-07 11:20 home/osboxes/temp_files/9.xml
                  21 2018-02-07 11:20 home/osboxes/temp_files/10.xml
                  --------- -------
                  201 10 files




                  • unzip -l <archive> - list archive files (short format)


                  Extracting the first 2 lines from each file in the archive:



                  unzip -c 10xml.zip | awk -v n=2 'NR==1 next /^ +inflating:/ n=NR+2; next n && NR<=n'



                  • unzip -c <archive> - extract files to stdout/screen. The name of each file is printed as it is extracted.






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 7 at 16:39









                  Jeff Schaller

                  31.3k846105




                  31.3k846105










                  answered Feb 7 at 9:51









                  RomanPerekhrest

                  22.4k12144




                  22.4k12144






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422472%2freading-first-n-lines-from-each-member-of-a-zip-archive%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay