Expansion of variables in sh scripts

Bill.Stewart wcs at ho95e.ATT.COM
Fri Feb 26 09:06:03 AEST 1988


In article <1159 at valhalla.ee.rochester.edu> badri at valhalla.ee.rochester.edu (Badri Lokanathan) writes:
>Well, after several years of shell script writing, I thought I knew
>everything about it, but I was wrong!
>Given a 2 column file of data in two columns, the columns separated
>by blanks. A script is desired which, among other things, searches
>for a word in the I column and outputs the corresponding entry in
>the II column.

(I realize your question was about why your awk script didn't get
passed the correct arguments.)  But why use awk at all?
It's very flexible, but much slower than egrep or sed.

For this application, I'd recommend 
	: Usage: myname pattern file
	egrep "^$1 " $2 | cut -f2 -d" "

Even if you decide to use awk instead of cut to extract the second
column (and presumably do summaries or other useful work), you'll
speed the program up significantly by using egrep to reduce the
amount of data that awk has to process.

Alternatively, you can write it in shell (which won't be real fast either.)
	: Usage: myname pattern file
	pattern="$1"; shift
	cat $* | while read col1 col2 ; do
		if [ "$col1" = "$pattern" ]
		then echo $col2
		fi
	done

If your shell doesn't provide test ([) as a builtin, use
case instead.
-- 
#				Thanks;
# Bill Stewart, AT&T Bell Labs 2G218, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs



More information about the Comp.unix.questions mailing list