Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

if i have a .gz file on unix which has certain number of lines. How could i count the lines on unix without uncompressing it.

share|improve this question

migrated from stackoverflow.com Apr 27 '10 at 21:38

This question came from our site for professional and enthusiast programmers.

    
Without extracting the archive you can't count the lines. –  zoli2k Apr 27 '10 at 7:38

3 Answers 3

You can obviously not count newlines if the file is still compressed.

But you can decompress to a stream, and count the newlines in that stream, without ever writing the (decompressed) file to disk. That would go something like so:

zcat file.gz | wc -l

zcat for decompress & cat, wc for wordcount. See man pages for both if you want to know more.

EDIT

If you do not have zcat, zcat is just another name for gunzip -c.

share|improve this answer
2  
On Unices where gzip is distinct from compress, you want gzcat. –  coneslayer Apr 27 '10 at 21:56

This also seems to work - grep for the number of line-endings in the file

zgrep -Ec "$" file.gz
share|improve this answer

If you want to do it quickly, I recommend using 'pigz' (which IIRC stands for "Parallel Implementation of GZip"). I just had a similar situation where I wanted to count the number of lines in a bunch of gzip'ed files and here was my solution:

for x in *.gz; do unpigz -p 8 -c $x | wc -l && echo $x; done

Which gave me the number of lines and the file it counted from on alternating lines, using 8 processors. It ran quickly!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.