How to find line count of zip files

April 14, 2009 at 12:59:11
Specs: KSH
Hi,
I have some data files in zipped format.I would like to count the number of rows in each zipped file.I am using KSH.

Thanks,


See More: How to find line count of zip files

Report •


#1
April 14, 2009 at 15:46:16
Depending on your environment zip has the zipinfo command. I'm assuming you want the total number of files.

Executing zipinfo foo.zip:

Archive: foo.zip 1051 bytes 6 files
-rwxrwxrwx 2.3 unx 126 tx defN 18-Mar-09 09:20 datafile.txt
-rw-r--r-- 2.3 unx 89 tx defN 12-Mar-09 13:35 mydatafile.txt
-rwxrwxrwx 2.3 unx 47 tx defN 18-Mar-09 08:52 myfile.txt
-rw-r--r-- 2.3 unx 126 tx defN 18-Mar-09 09:20 newdatafile.txt
-rw-r--r-- 2.3 unx 0 bx stor 19-Mar-09 12:52 file1.txt
-rw-r--r-- 2.3 unx 0 bx stor 19-Mar-09 12:52 file2.txt
6 files, 388 bytes uncompressed, 231 bytes compressed: 40.5%

The first line, second to last field has the total number of files in the archive - 6 in this case.

This script zipinfo's all the zip files in the current directory, grabs the second the last field of the first line using awk:

#!/bin/ksh

totcnt=0
for i in *.zip
do
   file_cnt=$(zipinfo $i| awk ' { cnt=NF-1; print $cnt; exit } ' )
   ((totcnt+=file_cnt))
done
echo "total number of files is: $totcnt"


Report •

#2
April 15, 2009 at 00:24:36
# set -- $(zipinfo -t test.zip)
# echo $1
6

or just the unzip tool
# unzip -l test.zip| awk 'END{print $2}'
6

Unix Win32 tools | Gawk for Windows


Report •

Related Solutions


Ask Question