home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 15886 < prev    next >
Encoding:
Text File  |  1993-01-24  |  2.3 KB  |  67 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!murphy!jpradley!jpr
  3. From: jpr@jpradley.jpr.com (Jean-Pierre Radley)
  4. Subject: Re: Simple scripting questions
  5. Date: Sat, 23 Jan 1993 20:32:38 GMT
  6. Message-ID: <1993Jan23.203238.2512@jpradley.jpr.com>
  7. References: <dremsen-220193161856@starmac5.mbl.edu>
  8. Organization: Unix in NYC
  9. Lines: 56
  10.  
  11. In article <dremsen-220193161856@starmac5.mbl.edu> dremsen@mbl.edu (David Remsen) writes:
  12. >I am trying to make a little shell script for a phone directory file.  I
  13. >want
  14. >to be able to ask the user for a string to search for and the field to
  15. >search.
  16. >The phone file is a comma delimited file so I want to use awk to
  17. >search a particular field.
  18. >
  19. >I assign the search input to a variable called find.  The problem is that
  20. >awk doesnt seem to be able to deal with this variable.
  21. >
  22. >awk -F: '$1 ~ /John/' phone.dat         - will work
  23. >awk -F: '$1 ~ ${find}' phone.dat          - will not work
  24. >
  25. >Can someone tell me why.  Ive tried all different permutations.  
  26.  
  27. Using an external variable as part of a pattern to be matched in awk requires
  28. tricky use of quotes. I use a ':'-delimited phonebook file, and retrieve lines
  29. like this:
  30.  
  31. : << 411
  32. get phone #s from /pub/lib/411 & $HOME/lib/411, containing colon-separated
  33. entries in format:
  34.     'lastname:firstname:telephone#:otherinfo'
  35. usage: 411  [key1 [key2 [...]]]
  36. examples: '411 mike' gives all MIKEs, '411 mike 201' gives only MIKEs in NJ,
  37. '411 home rad' gives only home numbers (if so coded), for RADleys, but also for
  38. any bRADs, homes on paRADe avenue, etc.
  39. 411
  40.  
  41. [ "$Tso" -a "$Tse" -a "$Tup" ] || {
  42.     Tso=`tput smso` Tse=`tput rmso` Tup=`tput cuu1`; }
  43. [ -f /pub/lib/411 ] && LIST="$LIST /pub/lib/411"
  44. [ -f $HOME/lib/411 ] && LIST="$LIST $HOME/lib/411"
  45. [ "$LIST" ] && echo "\t\tlooking in$LIST" ||
  46.     { echo "\t$Tso no telephone lists $Tse" && exit 1 ; }
  47.  
  48. while [ $1 ]
  49. do
  50.     KEY="${KEY+$KEY&&}/$1/"
  51.     shift
  52. done
  53.  
  54. mawk '
  55. BEGIN    { FS= ":" }
  56. '$KEY'    { if (!(FOUND%20))
  57.       printf "      "Tso"numero"Tse"\t       "Tso"first"Tse" "Tso"last"\
  58.         Tse"\t\t\t\t"Tso"...und so weiter"Tse
  59.       if (length($4)+length($1)<51)
  60.         printf "%80s"Tup"%12s %15s %s\n",$4,$3,$2,$1
  61.       else printf "%12s %15s %s\n%80s",$3,$2,$1" >","< "$4
  62.       FOUND++ }
  63. END    { if (!FOUND) print "\t"Tso" Sorry, unlisted number ... "Tse }
  64. ' Tso=$Tso Tse=$Tse Tup=$Tup $LIST
  65. -- 
  66. Jean-Pierre Radley   Unix in NYC   jpr@jpr.com   jpradley!jpr   CIS: 72160.1341
  67.