File: zipgrep

package info (click to toggle)
unzip 5.40-1
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 4,120 kB
  • ctags: 5,900
  • sloc: ansic: 40,977; cpp: 3,778; makefile: 1,384; asm: 1,228; sh: 133
file content (56 lines) | stat: -rw-r--r-- 1,180 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /bin/sh
# zipgrep: searches the given zip members for a string or pattern
# This shell script assumes that you have installed UnZip.

pat=""
opt=""
while test $# -ne 0; do
  case "$1" in
  -e | -f) opt="$opt $1"; shift; pat="$1";;
  -*)      opt="$opt $1";;
   *)      if test -z "$pat"; then
             pat="$1"
           else
             break;
           fi;;
  esac
  shift
done

if test $# = 0; then
  echo "usage: `basename $0` [egrep_options] pattern zipfile [members...]"
  echo searches the given zip members for a string or pattern
  exit 1
fi
zipfile="$1"; shift

list=0
silent=0
opt=`echo "$opt" | sed -e 's/ //g' -e 's/-//g'`
case "$opt" in
  *l*) list=1; opt=`echo $opt | sed s/l//`
esac
case "$opt" in
  *h*) silent=1
esac
if test -n "$opt"; then
  opt="-$opt"
fi

res=0
for i in `unzip -Z1 "$zipfile" ${1+"$@"}`; do
  if test $list -eq 1; then

    unzip -p-L "$zipfile" "$i" | egrep $opt "$pat" > /dev/null && echo $i
    r=$?
  elif test $silent -eq 1; then

    unzip -p-L "$zipfile" "$i" | egrep $opt "$pat"
    r=$?
  else
    unzip -p-L "$zipfile" "$i" | egrep $opt "$pat" | sed "s|^|${i}:|"
    r=$?
  fi
  test "$r" -ne 0 && res="$r"
done
exit $res