Greping through binary data with zgrep and gzcat

I periodically need to look for a given string in one or more compressed log files. Taking the time (and resources) to decompress each file on the file system takes time, especially when I don’t plan to leave the file uncompressed. When these situations arise, I turn to my good friends gzcat and zgrep.

The zgrep utility allows you to look for a pattern in one or more compressed files. If you want to find the string “target port down” in a set of logs named sanlogs*.gz, you can do the following:

$ zgrep “target port down” sanlogs*.gz

If zgrep finds the string, it will print it. Alternatively, you can use the gzcat utility to decompress the contents of a file prior to sending it to another tool for processing:

$ gzcat services.gz | tail -2
# 48557-49150 Unassigned
# 49151 IANA Reserved

Both tools are incredibly useful, and should be in every SysAdmins tool bag.

2 Comments

anon  on November 5th, 2009

There’s a companion util for .bz archives as well – i think it’s called bzcat.

Andy Paton  on November 6th, 2009

Pipe Viwer – PV
Another one for your toolbag, I’ve only just come across this myself. Nice IBM article on it
http://tinyurl.com/yzakrdr

Leave a Comment