Home | History | Annotate | Download | only in bzip2
      1 #!/bin/sh
      2 
      3 # Bzgrep wrapped for bzip2, 
      4 # adapted from zgrep by Philippe Troin <phil (at] fifi.org> for Debian GNU/Linux.
      5 ## zgrep notice:
      6 ## zgrep -- a wrapper around a grep program that decompresses files as needed
      7 ## Adapted from a version sent by Charles Levert <charles (at] comm.polymtl.ca>
      8 
      9 PATH="/usr/bin:$PATH"; export PATH
     10 
     11 prog=`echo $0 | sed 's|.*/||'`
     12 case "$prog" in
     13 	*egrep)	grep=${EGREP-egrep}	;;
     14 	*fgrep)	grep=${FGREP-fgrep}	;;
     15 	*)	grep=${GREP-grep}	;;
     16 esac
     17 pat=""
     18 while test $# -ne 0; do
     19   case "$1" in
     20   -e | -f) opt="$opt $1"; shift; pat="$1"
     21            if test "$grep" = grep; then  # grep is buggy with -e on SVR4
     22              grep=egrep
     23            fi;;
     24   -A | -B) opt="$opt $1 $2"; shift;;
     25   -*)	   opt="$opt $1";;
     26    *)      if test -z "$pat"; then
     27 	     pat="$1"
     28 	   else
     29 	     break;
     30            fi;;
     31   esac
     32   shift
     33 done
     34 
     35 if test -z "$pat"; then
     36   echo "grep through bzip2 files"
     37   echo "usage: $prog [grep_options] pattern [files]"
     38   exit 1
     39 fi
     40 
     41 list=0
     42 silent=0
     43 op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
     44 case "$op" in
     45   *l*) list=1
     46 esac
     47 case "$op" in
     48   *h*) silent=1
     49 esac
     50 
     51 if test $# -eq 0; then
     52   bzip2 -cdfq | $grep $opt "$pat"
     53   exit $?
     54 fi
     55 
     56 res=0
     57 for i do
     58   if test -f "$i"; then :; else if test -f "$i.bz2"; then i="$i.bz2"; fi; fi
     59   if test $list -eq 1; then
     60     bzip2 -cdfq "$i" | $grep $opt "$pat" 2>&1 > /dev/null && echo $i
     61     r=$?
     62   elif test $# -eq 1 -o $silent -eq 1; then
     63     bzip2 -cdfq "$i" | $grep $opt "$pat"
     64     r=$?
     65   else
     66     j=${i//\\/\\\\}
     67     j=${j//|/\\|}
     68     j=${j//&/\\&}
     69     j=`printf "%s" "$j" | tr '\n' ' '`
     70     bzip2 -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${j}:|"
     71     r=$?
     72   fi
     73   test "$r" -ne 0 && res="$r"
     74 done
     75 exit $res
     76