4 # zgrep -- a wrapper around a grep program that decompresses files as needed
5 # Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
7 # Copyright (C) 1998, 2001 Free Software Foundation
8 # Copyright (C) 1993 Jean-loup Gailly
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2, or (at your option)
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 PATH="BINDIR:$PATH"; export PATH
27 prog=`echo $0 | sed 's|.*/||'`
29 *egrep) grep=${EGREP-egrep} ;;
30 *fgrep) grep=${FGREP-fgrep} ;;
31 *) grep=${GREP-grep} ;;
37 files_without_matches=0
41 while test $# -ne 0; do
42 case "$after_dash_dash$1" in
43 --d* | --rec*) echo >&2 "$0: $1: option not supported"; exit 1;;
44 --files-with-*) files_with_matches=1;;
45 --files-witho*) files_without_matches=1;;
46 --no-f*) no_filename=1;;
47 --wi*) with_filename=1;;
51 -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 1;;
54 -*H*) with_filename=1;;
60 -*L*) files_without_matches=1;;
63 -*l*) files_with_matches=1;;
66 case "$after_dash_dash$1" in
67 -[ef]) opt="$opt $1"; shift; pat="$1"
68 if test "$grep" = grep; then # grep is buggy with -e on SVR4
71 -[ABCdm])opt="$opt $1 $2"; shift;;
72 --) opt="$opt $1"; after_dash_dash=1;;
74 *) if test -z "$pat"; then
83 if test -z "$pat"; then
84 echo "grep through gzip files"
85 echo "usage: $prog [grep_options] pattern [files]"
89 if test $# -eq 0; then
90 gzip -cdfq | $grep $opt "$pat"
97 if test $files_with_matches -eq 1; then
98 $grep $opt "$pat" > /dev/null && echo $i
99 elif test $files_without_matches -eq 1; then
100 $grep $opt "$pat" > /dev/null || echo $i
101 elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
104 if test $with_filename -eq 1; then
105 sed_script="s|^[^:]*:|${i}:|"
107 sed_script="s|^|${i}:|"
109 $grep $opt "$pat" | sed "$sed_script"
112 test $res -lt $r && res=$r