]> git.cworth.org Git - gzip/blob - zgrep.in
Imported Upstream version 1.3.2
[gzip] / zgrep.in
1 :
2 #!/bin/sh
3
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>
6
7 # Copyright (C) 1998, 2001 Free Software Foundation
8 # Copyright (C) 1993 Jean-loup Gailly
9
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)
13 # any later version.
14
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.
19
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
23 # 02111-1307, USA.
24
25 PATH="BINDIR:$PATH"; export PATH
26
27 prog=`echo $0 | sed 's|.*/||'`
28 case "$prog" in
29         *egrep) grep=${EGREP-egrep}     ;;
30         *fgrep) grep=${FGREP-fgrep}     ;;
31         *)      grep=${GREP-grep}       ;;
32 esac
33
34 pat=""
35 after_dash_dash=""
36 files_with_matches=0
37 files_without_matches=0
38 no_filename=0
39 with_filename=0
40
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;;
48   --*)  ;;
49   -*)
50         case "$1" in
51         -*[dr]*) echo >&2 "$0: $1: option not supported"; exit 1;;
52         esac
53         case "$1" in
54         -*H*)   with_filename=1;;
55         esac
56         case "$1" in
57         -*h*)   no_filename=1;;
58         esac
59         case "$1" in
60         -*L*)   files_without_matches=1;;
61         esac
62         case "$1" in
63         -*l*)   files_with_matches=1;;
64         esac;;
65   esac
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
69              grep=egrep
70            fi;;
71   -[ABCdm])opt="$opt $1 $2"; shift;;
72   --)      opt="$opt $1"; after_dash_dash=1;;
73   -*)      opt="$opt $1";;
74    *)      if test -z "$pat"; then
75              pat="$1"
76            else
77              break;
78            fi;;
79   esac
80   shift
81 done
82
83 if test -z "$pat"; then
84   echo "grep through gzip files"
85   echo "usage: $prog [grep_options] pattern [files]"
86   exit 1
87 fi
88
89 if test $# -eq 0; then
90   gzip -cdfq | $grep $opt "$pat"
91   exit $?
92 fi
93
94 res=0
95 for i do
96   gzip -cdfq "$i" |
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
102       $grep $opt "$pat"
103     else
104       if test $with_filename -eq 1; then
105         sed_script="s|^[^:]*:|${i}:|"
106       else
107         sed_script="s|^|${i}:|"
108       fi
109       $grep $opt "$pat" | sed "$sed_script"
110     fi
111   r=$?
112   test $res -lt $r && res=$r
113 done
114 exit $res