1996-05-12 16:20:26 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
1996-08-27 20:04:45 +00:00
|
|
|
# Copyright (c) April 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
|
|
|
|
# All rights reserved.
|
1996-05-12 16:20:26 +00:00
|
|
|
#
|
1996-08-27 20:04:45 +00:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL [your name] OR CONTRIBUTORS BE LIABLE
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
# SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
# zgrep - search possibly compressed files for a regular expression
|
|
|
|
#
|
1999-08-27 23:37:10 +00:00
|
|
|
# $FreeBSD$
|
1996-05-12 16:20:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
PATH=/bin:/usr/bin:$PATH; export PATH
|
|
|
|
|
|
|
|
# grep variant
|
|
|
|
case "$0" in
|
|
|
|
*egrep) grep=${EGREP-egrep} ;;
|
|
|
|
*fgrep) grep=${FGREP-fgrep} ;;
|
|
|
|
*) grep=${GREP-grep} ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
gzip=gzip gzipopt="-cdfq" pattern= grepopt= files= line= header=
|
|
|
|
|
|
|
|
# check options
|
1997-12-07 01:00:56 +00:00
|
|
|
while getopts "0123456789A:B:CEFGVX:bce:f:hiLlnqsvwxy" option
|
1996-05-12 16:20:26 +00:00
|
|
|
do
|
1997-12-07 01:00:56 +00:00
|
|
|
case "$option" in
|
|
|
|
e|f) pattern="-$option $OPTARG";; # -f file
|
|
|
|
l|L) line=-$option; grepopt="$grepopt -$option";;
|
|
|
|
h|q) header=-h; grepopt="$grepopt -$option";;
|
|
|
|
# rest
|
|
|
|
[0-9CEFGVbchiLlnqsvwxy]) grepopt="$grepopt -$option";;
|
|
|
|
[ABXef]) grepopt="$grepopt -$option $OPTARG";;
|
1996-05-12 16:20:26 +00:00
|
|
|
esac
|
|
|
|
done
|
1997-12-07 01:00:56 +00:00
|
|
|
shift $(($OPTIND - 1))
|
1996-05-12 16:20:26 +00:00
|
|
|
|
|
|
|
# check pattern
|
|
|
|
case X"$pattern" in
|
|
|
|
X) case $# in
|
|
|
|
0) echo "usage: $0 [grep_options] pattern [files]"; exit 1;;
|
|
|
|
*) pattern=$1; shift;;
|
|
|
|
esac
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
|
|
files="$@"
|
|
|
|
# no shell loop neccessary for option -q or -h
|
|
|
|
# and a single file or reading from stdin
|
|
|
|
case "$header"X"$#" in
|
1997-12-07 01:00:56 +00:00
|
|
|
-h*|-q*|X0|X1) $gzip $gzipopt -- $files | $grep $grepopt -- "$pattern"
|
1996-05-12 16:20:26 +00:00
|
|
|
exit $?;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
for f in $files
|
|
|
|
do
|
|
|
|
case "$line" in
|
1997-12-07 01:00:56 +00:00
|
|
|
-l) if $gzip $gzipopt -- $f |
|
|
|
|
$grep $grepopt -- "$pattern" >/dev/null
|
|
|
|
then
|
|
|
|
echo $f
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
-L) if $gzip $gzipopt -- $f |
|
|
|
|
$grep $grepopt -- "$pattern" >/dev/null; then :
|
|
|
|
else
|
|
|
|
echo $f
|
|
|
|
fi
|
|
|
|
;;
|
1996-05-12 16:20:26 +00:00
|
|
|
|
1997-12-07 01:00:56 +00:00
|
|
|
*) $gzip $gzipopt -- $f | $grep $grepopt -- "$pattern" |
|
|
|
|
sed "s%^%${f}:%"
|
|
|
|
;;
|
1996-05-12 16:20:26 +00:00
|
|
|
esac
|
|
|
|
done
|