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.
Quote:> .... 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.
different syntax of
(2), the other possible way is to declare awk a variable afterQuote:> wilma=`cat ${file1} | awk '{if($1== "'$fred'"){print("match")}}'
> or
> wilma=`cat ${file1} | awk "{if(\$1==\"$fred\"){print(\"match\")}}"
the end of the awk script (in the man pages, but I cant find it)
like so:
Quote:> wilma=`cat $file1 | awk '
> $1 == fredvar {printf("match on %s\n",fredvar)}
> ' fredvar="$fred"`
I've only tried the single quote method, and it works fine. Some ofQuote:> 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 '-'.
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 *ed one." |
| ..!ethz-inf!gaspar | along the way.In this proud land,you can't forget your|
\--------------------------------------------------------------------------