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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!think.com!enterpoop.mit.edu!bloom-picayune.mit.edu!news.mit.edu!jdell
  3. From: jdell@nefertiti.mit.edu (John Ellithorpe)
  4. Subject: Re: Simple scripting questions
  5. In-Reply-To: dremsen@mbl.edu's message of Fri, 22 Jan 93 21:25:13 GMT
  6. Message-ID: <JDELL.93Jan23122511@nefertiti.mit.edu>
  7. Followup-To: comp.unix.questions
  8. Sender: news@athena.mit.edu (News system)
  9. Nntp-Posting-Host: nefertiti.mit.edu
  10. Organization: Massachusetts Institute of Technology
  11. References: <dremsen-220193161856@starmac5.mbl.edu>
  12. Date: Sat, 23 Jan 1993 17:25:11 GMT
  13. Lines: 38
  14.  
  15. On Fri, 22 Jan 93 21:25:13 GMT, dremsen@mbl.edu (David Remsen) said:
  16. > Followup-To: comp.unix.questions
  17.  
  18. > I am trying to make a little shell script for a phone directory file.  I
  19. > want
  20. > to be able to ask the user for a string to search for and the field to
  21. > search.
  22. > The phone file is a comma delimited file so I want to use awk to
  23. > search a particular field.
  24.  
  25. > I assign the search input to a variable called find.  The problem is that
  26. > awk doesnt seem to be able to deal with this variable.
  27.  
  28. > awk -F: '$1 ~ /John/' phone.dat         - will work
  29. > awk -F: '$1 ~ ${find}' phone.dat          - will not work
  30.  
  31. > Can someone tell me why.  Ive tried all different permutations.  
  32.  
  33. I've run into this problem before.  The reason you are having problems is that
  34. you are confusing shell variables and awk variables.  Inside the single
  35. quotes, awk does treat ${find} as a shell variable.  The solution I've found
  36. (from the net of course) was:
  37.  
  38.     awk -F: '$1 ~ /'$find'/' phone.dat
  39.  
  40. The extra single quotes lets $find be seen by the shell (since it's not
  41. embedded in single quotes) and expanded before awk is executed.  The other
  42. way to do it is to use awk variables, but I've found the above method easier.
  43.  
  44. John
  45. --
  46.  
  47. ===============================================================================
  48. John Ellithorpe                           | Internet: jdell@maggie.mit.edu
  49. Dept. of Physics, Rm 26-349               | Phone   : (617) 253-3074  Office
  50. Massachusetts Institute of Technology     |           (617) 253-3072  Lab
  51. Cambridge, MA  02139                      |           (617) 236-4910  Home
  52. ===============================================================================
  53.