It is currently Tue, 30 Nov 2021 17:47:02 GMT



 
Author Message
 using /bin/sh vars in awk
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'|
| gas...@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|
| ga...@scri.fsu.edu | past,so dry your tears I say. No woman, No cry." -BMW |
\----------------------------------------------------------------------------/



 Sat, 18 Sep 1993 20:14:26 GMT   
 using /bin/sh vars in awk
In article <27...@neptune.inf.ethz.ch> gas...@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



 Thu, 07 Oct 1993 04:41:19 GMT   
 using /bin/sh vars in awk
#! /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  / n...@psg.com  / ..!uunet!m2xenix!news



 Fri, 08 Oct 1993 05:26:12 GMT   
 using /bin/sh vars in awk

        If your awk is nawk(New Awk), it's easy.

        ... | nawk '{if ($1 == fred) command}' fred="barney"

m.      
--
    Watanabe,Maki
    Finance/EIC
    Digital Equipment Corporation Japan



 Sat, 16 Oct 1993 08:58:59 GMT   
 using /bin/sh vars in awk
|>   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
j...@Athena.MIT.EDU                         Allston, MA  02134
Office: 617-253-8085                          Home: 617-782-0710



 Fri, 15 Oct 1993 20:27:14 GMT   
 using /bin/sh vars in awk
In article <1991Apr29.122714.17...@athena.mit.edu> j...@athena.mit.edu (Jonathan I. Kamens) writes:

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: mar...@mwtech.UUCP, voice: 49-(0)6151-6 56 83



 Sun, 17 Oct 1993 21:23:24 GMT   
 
   [ 6 post ] 

Similar Threads

1. SUMMARY: using sh vars in awk calls.

2. Maintaining white space in vars (/bin/sh)

3. sh vars in awk scripts??

4. Q: Sendmail Bug using From: |/bin/tail|/bin/sh

5. #!/bin/sh #!/usr/bin/sh can I do both for 2 diff machines

6. MINICOM and scripts using RUNSCRIPT, EXPECT and /bin/sh for automation

7. Using /bin/which from sh and ksh

8. Makefile's stoped when using it with SHELL=/bin/sh

9. sh script using awk, argument issue?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software