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