Count lines in a compressed file

command lineunix

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.

Best Answer

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.

Related Question