count lines in a compressed file

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.


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.


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

zgrep -Ec "$" file.gz