Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!cs.utexas.edu!convex!tchrist From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.unix.shell Subject: Re: does a zgrep exist? (zgrep <> zcat | grep) Message-ID: <1991Feb20.195441.1390@convex.com> Date: 20 Feb 91 19:54:41 GMT References: <1991Feb18.075330.15536@convex.com> <12845:Feb1900:14:4891@kramden.acf.nyu.ed <348@wybbs.mi.org> Sender: tchrist@convex.com (Tom Christiansen) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 69 From the keyboard of sleepy@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@convex.com convex!tchrist