Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!mit-eddie!uw-beaver!milton!sumax!polari!thebes!camco!bill From: bill@camco.Celestial.COM (Bill Campbell) Newsgroups: comp.unix.shell Subject: Re: does a zgrep exist? (zgrep <> zcat | grep) Message-ID: <750@camco.Celestial.COM> Date: 21 Feb 91 07:35:40 GMT References: <1991Feb15.232854.13378@robobar.co.uk> <463@bria> <1991Feb18.075330.15536@convex.com> Organization: Celestial Software, Mercer Island, WA Lines: 101 >>It would be nice to grep through compressed files. Sure, you can do zcat | >>grep regexp, but then you loose the ability of grep to tell you the >>filename and/or linenumber of a match. Uncompressing the files before >>greping isn't really wanted, cause you may not have the space on disk. >How quickly we forget! A perl solution might well be: while($file = shift) { $zcat = ($file =~ /\.Z$/ ? 'zcat' : $file =~ /\.z$/ ? 'pcat' : 'cat' ); open(INPUT, "$zcat $file"); while() { print "$file:$_\n"; } close(INPUT); } The one I actually use is: #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh 'zgrep' <<'END_OF_FILE' X: X# Header: %Z% %M% Version %I% %H% %T% %Z% X# exec [e|f]grep on a series of .Z files X Xprogname=`basename $0` XUSAGE="Usage: $progname [-v] [-e environment]" Xset -- `getopt FEvxclhnbsye: $*` Xif [ $? != 0 ] Xthen X echo $USAGE X exit 2 Xfi Xsuffix=$$ Xtmpenv=/tmp/tmpenv$suffix X> $tmpenv Xopts='' Xfor i in $* Xdo X case $i in X -F) prefix=f X shift;; X -E) prefix=e X shift;; X -[vxclhnbsy]) X opts="$opts $1" X shift 1;; X -e) X opts="$opts -e $2" X shift 2 ;; X --) shift; X break X ;; X esac Xdone X. $tmpenv # set any environment variables Xrm $tmpenv X Xopts="$opts '$1'" Xshift X Xif [ $# != 1 ] X then multi=true X else multi='' Xfi Xfor file do X case "$file" in X *.z) cat=pcat;; X *.Z) cat=zcat;; X *) cat=cat;; X esac X if [ "$multi" != "" ] X then eval "$cat $file | ${prefix}grep $opts" | sed "s;^;$file:;" X else eval "$cat $file | ${prefix}grep $opts" X fi Xdone END_OF_FILE if test 857 -ne `wc -c <'zgrep'`; then echo shar: \"'zgrep'\" unpacked with wrong size! fi chmod +x 'zgrep' # end of 'zgrep' fi echo shar: End of shell archive. exit 0 -- INTERNET: bill@Celestial.COM Bill Campbell; Celestial Software UUCP: ...!thebes!camco!bill 6641 East Mercer Way uunet!camco!bill Mercer Island, WA 98040; (206) 947-5591 Brought to you by Super Global Mega Corp .com