using /bin/sh vars in awk
andre
andre at targon.UUCP
Sun Apr 21 06:41:19 AEST 1991
In article <27814 at neptune.inf.ethz.ch> gaspar at inf.ethz.ch (Scott "gaspo" Gasparian) writes:
>Question: what syntax do I have to use to get awk to take /bin/sh
> variables in a shell script?
>Example: given a /bin/sh script, and an awk command in it somewheres,
> and i need to pass the var ${fred} to awk, what should
> the following line look like?
> fred="barney"
> wilma=`cat ${file1} | awk '{if($1== fred ){print("match")}}'
> ^^^^
> what I need is how to expand fred_| to get awk to
> replace it with the string "barney"
>
>Since this already came up, just email me the answer. I've already
>tried ${fred}, $fred, "${fred}" and a couple other combos.
Your problem is that you put the wak problam inside single ' quotes
and the shell will not expand variables in there no matter how you
try. (that's the good thing about single quotes).
If you want to expand a variable inside such a string, you must
end the quoted part, expand the variable and start the quotation
again. To be sure that it stays 1 argument if there are spaces
in the variable use double quotes to keep them together.
In your case this becomes:
wilma=`cat ${file1} | awk '{if($1== '"$fred"' ){print("match")}}'
hope this helps
--
The mail| AAA DDDD It's not the kill, but the thrill of the chase.
demon...| AA AAvv vvDD DD Ketchup is a vegetable.
hits!.@&| AAAAAAAvv vvDD DD {nixbur|nixtor}!adalen.via
--more--| AAA AAAvvvDDDDDD Andre van Dalen, uunet!hp4nl!targon!andre
More information about the Comp.unix.shell
mailing list