| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# Bzgrep wrapped for bzip2, |
| 4 |
# adapted from zgrep by Philippe Troin <[email protected]> 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 <[email protected]> |
| 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=$(echo "$i" | sed 's/\\/&&/g;s/|/\\&/g;s/&/\\&/g') |
| 67 |
j=`printf "%s" "$j" | tr '\n' ' '` |
| 68 |
# A trick adapted from |
| 69 |
# https://groups.google.com/forum/#!original/comp.unix.shell/x1345iu10eg/Nn1n-1r1uU0J |
| 70 |
# that has the same effect as the following bash code: |
| 71 |
# bzip2 -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${j}:|" |
| 72 |
# r=${PIPESTATUS[1]} |
| 73 |
exec 3>&1 |
| 74 |
eval ` |
| 75 |
exec 4>&1 >&3 3>&- |
| 76 |
{ |
| 77 |
bzip2 -cdfq "$i" 4>&- |
| 78 |
} | { |
| 79 |
$grep $opt "$pat" 4>&-; echo "r=$?;" >&4 |
| 80 |
} | sed "s|^|${j}:|" |
| 81 |
` |
| 82 |
fi |
| 83 |
test "$r" -ne 0 && res="$r" |
| 84 |
done |
| 85 |
exit $res |