gaspar@inf.ethz.ch (Scott "gaspo" Gasparian) (04/02/91)
ok, I know this came up awhile ago, and I dont want to bring it up again. However, not enuf disk to keep articles around, and I forgot the answer: 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. thanks --gaspo. BTW, the example is just that, I don't need a different way to solve the example. just get the vars to awk. /----------------------------------------------------------------------------\ | Scott "gaspo" Gasparian -- System Administrator | _>________ _<________ | | Dept. Informatik, Eidg. Techn. Hochschule, Zurich |/[][][][][]\/[][][][][]\| | ETH-Zentrum, CH-8092 Zurich. T# 01-01-254-7205 |`oo------oo'`oo------oo'| | 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 | \----------------------------------------------------------------------------/
andre@targon.UUCP (andre) (04/21/91)
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
news@m2xenix.psg.com (Randy Bush) (04/22/91)
#! /bin/sh # awk "{if (substr(\$1,1,3)==192) {print \"$1 \" \$2 \" $2 $3 $4 $5 $6 $7 $8 $9\" }}" /etc/hosts | sort | /bin/sh -v -- Randy Bush / news@psg.com / ..!uunet!m2xenix!news
jik@athena.mit.edu (Jonathan I. Kamens) (04/29/91)
In article <MAKI.91Apr29205859@void.jit.dec.com>, maki@void.jit.dec.com (Watanabe Maki) writes: |> If your awk is nawk(New Awk), it's easy. |> |> ... | nawk '{if ($1 == fred) command}' fred="barney" Actually, this syntax will work with nearly any version of awk, not just nawk. It was available, albeit undocumented, in even the earliest distributed versions. -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710
martin@mwtech.UUCP (Martin Weitzel) (05/01/91)
In article <1991Apr29.122714.17138@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes: >In article <MAKI.91Apr29205859@void.jit.dec.com>, maki@void.jit.dec.com (Watanabe Maki) writes: >|> If your awk is nawk(New Awk), it's easy. >|> >|> ... | nawk '{if ($1 == fred) command}' fred="barney" > > Actually, this syntax will work with nearly any version of awk, not just >nawk. It was available, albeit undocumented, in even the earliest distributed >versions. Minor nitpick: But be sure to add a "-" if you want to have stdin processed. ... | nawk '{if ($1 == fred) command}' fred="barney" - # Important little thing here, watch closely! -------^ Reason: awk in its older versions is a bit stupid about the command line arguments that are assignments to awk-variables and counts them as if they were file arguments, failing process stdin as default if there are any such assignments but no file arguments. The trick with explicitly naming stdin solves this problem. -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83