gaspar@inf.ethz.ch (Scott "gaspo" Gasparian) (04/05/91)
greetings all! Ok, thanks to all who replied, and since I got a couple "me toos", heres the deal: Problem: in a /bin/sh script, need to pass a regular sh variable to the awk call, but the proper syntax is not-readily discernable. Example: given sh vars $fred, call awk using fred, like so: wilma=`cat $filea | awk '{if($1==fred){print("match")}}'` Solution: (1), escape out the sh variable by using single quotes. rhartman@thestepchild.esd.sgi.com (Robert Hartman) says: > > .... awk '{ ... '$fred' ...}' ... > >Since single-quotes suppress variable substitution, if you want the shell >to expand the reference you have to leave it outside. Note that >the whole awk argument is still treated as one word by the shell, since >there is no white space between the single quotes and the expanded shell >variable. Kenneth Herron <kherron@ms.uky.edu> agrees with a slightly different syntax of > > wilma=`cat ${file1} | awk '{if($1== "'$fred'"){print("match")}}' > or > wilma=`cat ${file1} | awk "{if(\$1==\"$fred\"){print(\"match\")}}" (2), the other possible way is to declare awk a variable after the end of the awk script (in the man pages, but I cant find it) like so: dwn@swbatl.sbc.com (David Neill-OKCy Mktg 405-278-4007) suggests > wilma=`cat $file1 | awk ' > $1 == fredvar {printf("match on %s\n",fredvar)} > ' fredvar="$fred"` and andyb@coat.com (Andy Behrens) explains: > fred="barney" > wilma=` cat ${file1} | > awk '{ > if ($1 == fred {print("match")} > }' fred=${fred} - ` > >Awk allows command line assignments of the form 'name=value'. These >assignments follow the awk script and precede the filenames. In most >versions of awk, you must have at least one filename on the command >line. If you want awk to read from standard input (as in the above >example), you can use a filename of '-'. I've only tried the single quote method, and it works fine. Some of this is supposed to be dependent on your version of awk. dont ask, i dont know. Thanks for all the info! --gaspo. BTW, to the dude who thought my sig was "long and obnoxious", you wouldnt like my hair either! /----------------------------------------------------------------------------\ | Scott "gaspo" Gasparian -- System Administrator |"Do you go every Sunday?| | Dept. Informatik, Eidg. Techn. Hochschule, Zurich | sounds to me like YOU | | ETH-Zentrum, CH-8092 Zurich. T# 01-01-254-7205 | are the addicted one." | | gaspar@inf.ethz.ch | "Good friends we've had, or good friends we've lost, | | ..!ethz-inf!gaspar | along the way.In this proud land,you can't forget your| | gaspo@scri.fsu.edu | past,so dry your tears I say. No woman, No cry." -BMW | \----------------------------------------------------------------------------/