How can I decompress and print the last few lines of a compressed text file?
You can't, as it has been already said, if the files have been compressed with standard gzip
. If you have control over the compression, you can use dictzip
to compress the files, it compresses the files in separate blocks and you can decompress just the last block (typically 64KB). And it is backward compatible with gzip
, meaning the dictzipped file is perfectly legal gzipped file as well.
Other possibility would be if you get the gzipped file as a concatenation of several already gzipped files, you could search for the last gzip signature and decompress everything after that.
Well, you can access randomly a gzipped file if you previously creates an index for each file ...
I've developed the command line tool that you were probably looking for: it can access the tail using the same amount of time than a gunzip... BUT because it creates a little (<<1%/gzip) index, next extractions will be really quick:
https://github.com/circulosmeos/gztool
The tool has two options that may be of interest for you:
- -S option supervise a still-growing file and creates an index for it as it is growing - this can be useful for gzipped rsyslog files as reduces to zero in the practice the time of index creation.
- -t tails a gzip file: this way you can do:
$ gztool -t foo.gz
Please, note that if the index doesn't exists, this will consume the same time as a complete decompression: but as the index is reusable, next searches will be greatly reduced in time - and as it is the same time, why not use it and create the index at the same time?
This tool is based on zran.c demonstration code from original zlib, so there's no out-of-the-rules magic!