does a zgrep exist? (zgrep <> zcat | grep)
Bill Campbell
bill at camco.Celestial.COM
Thu Feb 21 18:35:40 AEST 1991
>>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(<INPUT>) {
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 <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: zgrep
# Wrapped by bill at camco on Wed Feb 20 23:34:27 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'zgrep' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'zgrep'\"
else
echo shar: Extracting \"'zgrep'\" \(857 characters\)
sed "s/^X//" >'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 at Celestial.COM Bill Campbell; Celestial Software
UUCP: ...!thebes!camco!bill 6641 East Mercer Way
uunet!camco!bill Mercer Island, WA 98040; (206) 947-5591
More information about the Comp.unix.shell
mailing list