This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / gnu / usr.bin / gzip / zgrep
CommitLineData
caed0dfe
NW
1#!/bin/sh
2
3# zgrep -- a wrapper around a grep program that decompresses files as needed
4# Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
5
6PATH="/usr/local/bin:$PATH"; export PATH
7
8prog=`echo $0 | sed 's|.*/||'`
9case "$prog" in
10 *egrep) grep=${EGREP-egrep} ;;
11 *fgrep) grep=${FGREP-fgrep} ;;
12 *) grep=${GREP-grep} ;;
13esac
14A=
15fileno=0
16pat=""
17while test $# -ne 0; do
18 case "$1" in
19 -e | -f) opt="$opt $1"; shift; pat="$1"
20 if test "$grep" = grep; then # grep is buggy with -e on SVR4
21 grep=egrep
22 fi;;
23 -*) opt="$opt $1";;
24 *) if test -z "$pat"; then
25 pat="$1"
26 else
27 fileno=`expr $fileno + 1`
28 eval A$fileno=\$1
29 A="$A \"\$A$fileno\""
30 fi
31 ;;
32 esac
33 shift
34done
35
36if test -z "$pat"; then
37 echo "grep through gzip files"
38 echo "usage: $prog [grep_options] pattern [files]"
39 exit 1
40fi
41
42list=0
43silent=0
44op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
45case "$op" in
46 *l*) list=1
47esac
48case "$op" in
49 *h*) silent=1
50esac
51
52if test $fileno -eq 0; then
53 gzip -cdfq | $grep $opt "$pat"
54 exit $?
55fi
56eval set "$A" # files in $1, $2 ...
57
58res=0
59for i do
60 if test $list -eq 1; then
61 gzip -cdfq "$i" | $grep $opt "$pat" > /dev/null && echo $i
62 r=$?
63 elif test $# -eq 1 -o $silent -eq 1; then
64 gzip -cdfq "$i" | $grep $opt "$pat"
65 r=$?
66 else
67 gzip -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${i}:|"
68 r=$?
69 fi
70 test "$r" -ne 0 && res="$r"
71done
72exit $res