home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!murphy!jpradley!jpr
- From: jpr@jpradley.jpr.com (Jean-Pierre Radley)
- Subject: Re: Simple scripting questions
- Date: Sat, 23 Jan 1993 20:32:38 GMT
- Message-ID: <1993Jan23.203238.2512@jpradley.jpr.com>
- References: <dremsen-220193161856@starmac5.mbl.edu>
- Organization: Unix in NYC
- Lines: 56
-
- In article <dremsen-220193161856@starmac5.mbl.edu> dremsen@mbl.edu (David Remsen) writes:
- >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.
-
- Using an external variable as part of a pattern to be matched in awk requires
- tricky use of quotes. I use a ':'-delimited phonebook file, and retrieve lines
- like this:
-
- : << 411
- get phone #s from /pub/lib/411 & $HOME/lib/411, containing colon-separated
- entries in format:
- 'lastname:firstname:telephone#:otherinfo'
- usage: 411 [key1 [key2 [...]]]
- examples: '411 mike' gives all MIKEs, '411 mike 201' gives only MIKEs in NJ,
- '411 home rad' gives only home numbers (if so coded), for RADleys, but also for
- any bRADs, homes on paRADe avenue, etc.
- 411
-
- [ "$Tso" -a "$Tse" -a "$Tup" ] || {
- Tso=`tput smso` Tse=`tput rmso` Tup=`tput cuu1`; }
- [ -f /pub/lib/411 ] && LIST="$LIST /pub/lib/411"
- [ -f $HOME/lib/411 ] && LIST="$LIST $HOME/lib/411"
- [ "$LIST" ] && echo "\t\tlooking in$LIST" ||
- { echo "\t$Tso no telephone lists $Tse" && exit 1 ; }
-
- while [ $1 ]
- do
- KEY="${KEY+$KEY&&}/$1/"
- shift
- done
-
- mawk '
- BEGIN { FS= ":" }
- '$KEY' { if (!(FOUND%20))
- printf " "Tso"numero"Tse"\t "Tso"first"Tse" "Tso"last"\
- Tse"\t\t\t\t"Tso"...und so weiter"Tse
- if (length($4)+length($1)<51)
- printf "%80s"Tup"%12s %15s %s\n",$4,$3,$2,$1
- else printf "%12s %15s %s\n%80s",$3,$2,$1" >","< "$4
- FOUND++ }
- END { if (!FOUND) print "\t"Tso" Sorry, unlisted number ... "Tse }
- ' Tso=$Tso Tse=$Tse Tup=$Tup $LIST
- --
- Jean-Pierre Radley Unix in NYC jpr@jpr.com jpradley!jpr CIS: 72160.1341
-