home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / bin / zgrep < prev    next >
Text File  |  1994-12-22  |  1KB  |  67 lines

  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.  
  6. PATH="/gnu/bin:$PATH"; export PATH
  7.  
  8. prog=`echo $0 | sed 's|.*/||'`
  9. case "$prog" in
  10.     *egrep)    grep=${EGREP-egrep}    ;;
  11.     *fgrep)    grep=${FGREP-fgrep}    ;;
  12.     *)    grep=${GREP-grep}    ;;
  13. esac
  14. pat=""
  15. while test $# -ne 0; do
  16.   case "$1" in
  17.   -e | -f) opt="$opt $1"; shift; pat="$1"
  18.            if test "$grep" = grep; then  # grep is buggy with -e on SVR4
  19.              grep=egrep
  20.            fi;;
  21.   -*)       opt="$opt $1";;
  22.    *)      if test -z "$pat"; then
  23.          pat="$1"
  24.        else
  25.          break;
  26.            fi;;
  27.   esac
  28.   shift
  29. done
  30.  
  31. if test -z "$pat"; then
  32.   echo "grep through gzip files"
  33.   echo "usage: $prog [grep_options] pattern [files]"
  34.   exit 1
  35. fi
  36.  
  37. list=0
  38. silent=0
  39. op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
  40. case "$op" in
  41.   *l*) list=1
  42. esac
  43. case "$op" in
  44.   *h*) silent=1
  45. esac
  46.  
  47. if test $# -eq 0; then
  48.   gzip -cdfq | $grep $opt "$pat"
  49.   exit $?
  50. fi
  51.  
  52. res=0
  53. for i do
  54.   if test $list -eq 1; then
  55.     gzip -cdfq "$i" | $grep $opt "$pat" > /dev/null && echo $i
  56.     r=$?
  57.   elif test $# -eq 1 -o $silent -eq 1; then
  58.     gzip -cdfq "$i" | $grep $opt "$pat"
  59.     r=$?
  60.   else
  61.     gzip -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${i}:|"
  62.     r=$?
  63.   fi
  64.   test "$r" -ne 0 && res="$r"
  65. done
  66. exit $res
  67.