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>
6 # Copyright (C) 1998, 2001, 2002, 2006 Free Software Foundation
7 # Copyright (C) 1993 Jean-loup Gailly
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 PATH="BINDIR:$PATH"; export PATH
26 *egrep) grep=${EGREP-egrep} ;;
27 *fgrep) grep=${FGREP-fgrep} ;;
28 *) grep=${GREP-grep} ;;
31 version="z$grep (gzip) @VERSION@
32 Copyright (C) 2006 Free Software Foundation, Inc.
33 This is free software. You may redistribute copies of it under the terms of
34 the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
35 There is NO WARRANTY, to the extent permitted by law.
37 Written by Jean-loup Gailly."
39 usage="Usage: z$grep [OPTION]... [-e] PATTERN [FILE]...
40 Look for instances of PATTERN in the input FILEs, using their
41 uncompressed contents if they are compressed.
43 OPTIONs are the same as for '$grep'.
45 Report bugs to <bug-gzip@gnu.org>."
50 files_without_matches=0
54 while test $# -ne 0; do
55 case "$after_dash_dash$1" in
56 --d* | --rec*) echo >&2 "$0: $1: option not supported"; exit 2;;
57 --h*) echo "$usage" || exit 2; exit;;
58 --files-with-*) files_with_matches=1;;
59 --files-witho*) files_without_matches=1;;
60 --no-f*) no_filename=1;;
61 --v*) echo "$version" || exit 2; exit;;
62 --wi*) with_filename=1;;
66 -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 2;;
69 -*H*) with_filename=1;;
75 -*L*) files_without_matches=1;;
78 -*l*) files_with_matches=1;;
81 case "$after_dash_dash$1" in
82 -[ef]) opt="$opt $1"; shift; pat="$1"
83 if test "$grep" = grep; then # grep is buggy with -e on SVR4
86 -[ABCdm])opt="$opt $1 $2"; shift;;
87 --) opt="$opt $1"; after_dash_dash=1;;
89 *) if test -z "$pat"; then
98 if test -z "$pat"; then
103 if test $# -eq 0; then
104 gzip -cdfq | $grep $opt "$pat"
111 if test $files_with_matches -eq 1; then
112 $grep $opt "$pat" > /dev/null && printf '%s\n' "$i"
113 elif test $files_without_matches -eq 1; then
114 $grep $opt "$pat" > /dev/null || printf '%s\n' "$i"
115 elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
125 *'&'*) char='&' repl='\&';;
126 *'\'*) char='\\' repl='\\';;
127 *'|'*) char='|' repl='\|';;
130 up_to_first_char="\\([^$char]*\\)"
131 after_first_char="[^$char]*$char\\(.*\\)"
132 escaped=$escaped`expr "X$i" : "X$up_to_first_char"`$repl
133 i=`expr "X$i" : "$after_first_char"`
135 if test $with_filename -eq 1; then
136 sed_script="s|[^:]*|$escaped$i|"
138 sed_script="s|^|$escaped$i:|"
141 # Fail if either grep or sed fails.
142 # Bash has ${PIPESTATUS[0]}, but that's not portable.
146 ($grep $opt "$pat" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
151 test $res -lt $r && res=$r