Reading first N lines from each member of a zip archive
Clash 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?
text-processing zip
add a comment |Â
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?
text-processing zip
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
add a comment |Â
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?
text-processing zip
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?
text-processing zip
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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.
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
add a comment |Â
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.
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Feb 7 at 16:56
Jeff Schaller
31.3k846105
31.3k846105
add a comment |Â
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Feb 7 at 16:39
Jeff Schaller
31.3k846105
31.3k846105
answered Feb 7 at 9:51
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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