Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uwm.edu!lll-winken!elroy.jpl.nasa.gov!sdd.hp.com!think.com!mintaka!bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!targon!andre From: andre@targon.UUCP (andre) Newsgroups: comp.unix.shell Subject: Re: using /bin/sh vars in awk Message-ID: <1695@targon.UUCP> Date: 20 Apr 91 20:41:19 GMT References: <27814@neptune.inf.ethz.ch> Distribution: comp.unix.shell Organization: Siemens Nixdorf Informationsystems BV.,SWZ, Vianen, the Netherlands. Lines: 37 In article <27814@neptune.inf.ethz.ch> gaspar@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