bzgrep 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/sh
  2. # Bzgrep wrapped for bzip2,
  3. # adapted from zgrep by Philippe Troin <phil@fifi.org> for Debian GNU/Linux.
  4. ## zgrep notice:
  5. ## zgrep -- a wrapper around a grep program that decompresses files as needed
  6. ## Adapted from a version sent by Charles Levert <charles@comm.polymtl.ca>
  7. PATH="/usr/bin:$PATH"; export PATH
  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. -A | -B) opt="$opt $1 $2"; shift;;
  22. -*) opt="$opt $1";;
  23. *) if test -z "$pat"; then
  24. pat="$1"
  25. else
  26. break;
  27. fi;;
  28. esac
  29. shift
  30. done
  31. if test -z "$pat"; then
  32. echo "grep through bzip2 files"
  33. echo "usage: $prog [grep_options] pattern [files]"
  34. exit 1
  35. fi
  36. list=0
  37. silent=0
  38. op=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
  39. case "$op" in
  40. *l*) list=1
  41. esac
  42. case "$op" in
  43. *h*) silent=1
  44. esac
  45. if test $# -eq 0; then
  46. bzip2 -cdfq | $grep $opt "$pat"
  47. exit $?
  48. fi
  49. res=0
  50. for i do
  51. if test -f "$i"; then :; else if test -f "$i.bz2"; then i="$i.bz2"; fi; fi
  52. if test $list -eq 1; then
  53. bzip2 -cdfq "$i" | $grep $opt "$pat" 2>&1 > /dev/null && echo $i
  54. r=$?
  55. elif test $# -eq 1 -o $silent -eq 1; then
  56. bzip2 -cdfq "$i" | $grep $opt "$pat"
  57. r=$?
  58. else
  59. j=${i//\\/\\\\}
  60. j=${j//|/\\|}
  61. j=${j//&/\\&}
  62. j=`printf "%s" "$j" | tr '\n' ' '`
  63. bzip2 -cdfq "$i" | $grep $opt "$pat" | sed "s|^|${j}:|"
  64. r=$?
  65. fi
  66. test "$r" -ne 0 && res="$r"
  67. done
  68. exit $res