does a zgrep exist? (zgrep <> zcat | grep)

Tom Christiansen tchrist at convex.COM
Thu Feb 21 06:54:41 AEST 1991


>From the keyboard of sleepy at wybbs.UUCP (Mike Faber):
:a simple shell invoked with arguements is easy enough...
:
:$ sh zgrep string file [file ...]
:
:zgrep:
:if [ $# -lt 2]
:then
:	echo usage:
:	exit 1
:fi
:
:vgrep=$1
:shift
:
:while [ $# -gt 0 ]
:do
:	zcat $i | grep $vgrep | awk fil=$1 '{printf("%s:%s\n",fil,$0)}'
:	shift
:done
:
:Now, was that so painful...

Apparently.  First, it's buggy (see below).  Second, it's not extensible:
you should make the "zcat" part vary, because someday someone will want to
use "pcat" instead of "zcat" or "nm" or "strings".  Third, grep's regexps
are weak; use egrep at the very least.  Fourth, it's slow.

As for the bugs:

    if [ $# -lt 2]
should be
    if [ $# -lt 2 ]

And this line:

    zcat $i | grep $vgrep | awk fil=$1 '{printf("%s:%s\n",fil,$0)}'

has three (3) bugs in it: 

    1) the $i should be $1
    2) The $vgrep should be "$vgrep" so it doesn't retokenize.
    3) Neither awk, gawk, nor nawk will swallow that file=$1 code.

The corrected line reads:

       zcat $1 | grep "$vgrep" | awk "{printf(\"$1:%s\n\",\$0)}"

Now let's consider timing.  I have 38 compressed files totalling around
80k in /usr/man/man1/a*.Z; watch:  (and yes, I used GNU grep.)

% time sh zgrep 'file* system' /usr/man/man1/a*.Z > /dev/null
2.1u 7.7s 0:14 67% 0+0k 28+0io 2245pf+0w

% time perl pipegrep 'file *system' zcat /usr/man/man1/a*.Z > /dev/null
1.1u 2.1s 0:05 66% 0+0k 29+0io 658pf+0w

'Nuff said?

The pipegrep program, as I mentioned earlier, is described in the
O'Reilly Camel Book on perl, and available via anon FTP inside of
nutshell/perl/perl.tar.Z on uunet.

--tom
-- 
"UNIX was not designed to stop you from doing stupid things, because
 that would also stop you from doing clever things" -- Doug Gwyn

 Tom Christiansen                tchrist at convex.com      convex!tchrist



More information about the Comp.unix.shell mailing list