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