|
``Wizard's Grabbag'': Column No. 003: Listing |
1 #!/bin/sh
2 # @(#) msearch Search mail folders for pattern
3 # By default, the mbox file in the user's home directory is searched.
4
5 test $# -eq 0 && { echo "usage: $0 pattern... [- file...]" ; exit 1; }
6
7 # Use pattern arguments to generate a set of awk statements to search
8 # for the patterns. The 'sed' command escapes any slashes
9 # in the pattern for awk.
10
11 i=0
12 for pat
13 do
14 test $pat = - && break
15 i=`expr $i + 1`
16 awkstmts="$awkstmts /`echo $pat|sed 's,/,\\\/,g'`/{ found[$i] = 1 }"
17 shift
18 done
19 npat=$i
20
21 # Set $files to the list of input files, and $printname to 1
22 # if each line should be prepended with the file name
23 # that is, if more than one input file).
24
25 printname=0
26 if test "$1" = -
27 then
28 # user-specified file name(s)
29 test $# -gt 1 && shift
30 files=$*
31 test $# -gt 1 && printname=1
32 else
33 # default mail file
34 files=${MBOX-$HOME/mbox}
35 test ! -f $files &&
36 { echo $0: cannot find mbox file $files ; exit 1; }
37 fi
38
39 # Search email files, print result
40 for file in $files
41 do
42 { cat $file; echo 'From '; } |
43 awk "
44 /^From /{
45 n = 0
46 for (i=1; i <= $npat; i++) {
47 n += found[i]
48 found[i] = 0
49 }
50 if (n==$npat && msgno) {
51 if ($printname==1)
52 printf(\"%s \", \"$file\")
53 print msgno, from, subject
54 }
55 from = \"\"
56 subject = \"\"
57 msgno++
58 }
59 /^From: / && from == \"\" {
60 from = substr(\$0, 7)
61 }
62 /^Subject: / && subject==\"\" {
63 subject = substr(\$0, 10)
64 }
65 $awkstmts
66 "
67 done
|
Print This Page Send as e-mail |












