home *** CD-ROM | disk | FTP | other *** search
- #
- # phfind.awk -- do searches against phone/address database
- #
- # required file format: comma delimited fields, \n delimited records.
- #
- # $1 == name (any notation, no commas)
- # $2 == phone (any notation, no commas: e.g. (111) 222-3333 or
- # 111-222-3333)
- # $3 and greater == anything at all, commas allowed
- #
- # example legal entries:
- #
- # post emily, 111-222-3333, what, great, manners
- # george, (111) 222 3333
- # emily carr, , 12 Boatswain Lane, Essex
- #
- #
- # author: Richard J. Reiner, rreiner@vm1.yorku.ca, rreiner@yorkvm1.bitnet,
- # grad3077@writer.yorku.ca. 28 Nov 89.
- #
- # License for personal non-commercial use is granted. Any commercial
- # industrial, or governmental use is forbidden without express permission
- # from the author.
- #
- # Written and tested with Rob Duff's AWK v2.12.
- #
-
- BEGIN \
- {
- FS=","
-
- parseargs()
-
- dialprefix="ATDP" # define modem command strings
- dialsuffix="\n"
- }
-
- $0 ~ s \
- {
- if (f != 0) # full record is wanted
- {
- printf("%s\n",$0)
- }
- else if (d != 0) # dial command wanted
- {
- printf("%s%s%s",dialprefix,cleanphonenum($2),dialsuffix)
- }
- else # default: name and phone number only
- {
- printf("%s's phone number is %s\n",$1,$2)
- }
-
- if(++hits == n) # abort if max reached
- exit(0)
- }
-
-
-
- # read and parse argv. i is local.
- function parseargs(i) \
- {
- if (ARGC < 2) # we don't have even a -s
- usageandexit()
-
- for(i=ARGC-1; i>0; i--)
- {
- if (ARGV[i]=="-d")
- d=1
- else if (ARGV[i]=="-f")
- f=1
- else if (index(ARGV[i],"-s")==1)
- s=substr(ARGV[i],3)
- else if (index(ARGV[i],"-n")==1)
- n=substr(ARGV[i],3)
- else
- {
- printf("bad option: %s\n",ARGV[i])
- usageandexit()
- }
- }
-
- ARGC=1 # don't try to open switches as files
-
- # make n==1 the default. Also, force n==1 if we have -d.
- if ((d != 0) || (n == 0))
- n=1
- }
-
-
- # strip nonsense out of phone numbers, for dialer. Also kill area code,
- # so we don't get accidental LD calls.
- function cleanphonenum(ph) \
- {
- gsub("[ ()-]","",ph) # remove spaces, lparens, rparens, hyphens
-
- if ((length(ph)!=7) && (length(ph)!=10)) # wrong number of digits or blank
- return("malformed or unknown")
- else
- {
- if (length(ph) == 10)
- {
- ph=substr(ph, 4) # remove area code
- }
- }
- return (ph)
- }
-
-
- # show usage and then die with error status
- function usageandexit() \
- {
- printf("usage: awk phf {options} <addrfile\n")
- printf("options: -sEXP expression to match\n")
- printf(" -nNUM stop after printing n entries (default 1)\n")
- printf(" -d print modem dial command\n")
- printf(" -f print full record\n")
- exit(1)
- }
-