I can grep both compressed files and uncompressed files using the same command

I am trying to make a script for grepping log files located in / var / log /. I can either grep uncompressed logs or compressed logs (as a result of log rotation), but could not perform both actions in one command (I get "Binary file (standard input)" when I try to do this, is it possible to do this? thanks!

+7
source share
3 answers

Try to do this using the function:

greplog () { cd /var/log { cat $1 $1.*[0-9] zcat $1.*.gz } | grep "$2" } 

Using:

 $ greplog syslog pattern 
+6
source

zgrep can do this for you, it processes compressed and uncompressed files. One of the drawbacks is that it cannot recursively process directories. But for your example, this is quite enough, and you can filter, for example. syslog as follows:

 $ zgrep PATTERN /var/log/syslog* 
+7
source

xzgrep is a one-stop shop for commonly compressed files, at least on Ubuntu 16 and macOS 10.12 (installed with xz from MacPorts ). On the man page: xzgrep invokes grep(1) on files which may be either uncompressed or compressed with xz(1), lzma(1), gzip(1), or bzip2(1) .

Usage (-r note is not supported):

$ find . -type f | parallel -j4 'xzgrep -Hn "PATTERN" {}'

+1
source

All Articles