passing vars to awk script 
Author Message
 passing vars to awk script

Quote:

> I'm sure this is something I should be able to figure out but...
> How do you pass variables into an awk script calling the awk from a korn shell
> script?  I've tried:
> awk -f awkscript var1 var2 filename
> awk -f awkscript -v one=var1 -v two=var2 filename
> .. and then receiving them in the awk script as $1 and $2 in either the BEGIN
> section or as the first line in the action section.
> Neither works.  I need to do it with awk -- not nawk.

1) old awk doesn't support -v, to my knowledge.  That means that you
can't have access to the variables in the BEGIN section.

2) The correct syntax in that case is:

awk -f awkscript var1=one var2=two filename

And awkscript contains:

------------------------------------------------------------------------
BEGIN { print "var1 is \"", var1, "\"" }
/foo/ { print var1 }
/bar/ { print var2 }
------------------------------------------------------------------------

which for an input file consisting of the lines "foo" and "bar" produces:

------------------------------------------------------------------------
var1 is ""
one
two
------------------------------------------------------------------------

HTH.
--
Dom Mitchell -- Palmer & Harvey McLane -- Unix Systems Administrator
``Damn the philosophy, just provide the functionality and let anyone
  with a competing philosophy come up with some better alternative if
  they don't like it.'' -- JKH



Sat, 12 May 2001 03:00:00 GMT  
 passing vars to awk script


Quote:

>#!/bin/ksh
>awk '
>BEGIN {
>myFirstVar='$1'
>mySecondVar='$2'}
>{
># program goes here using myFirstVar and mySecondVar variables
>}'

This works if $1 and $2 are representations of numbers, but not if they're
text. For example, if $1 were "hi", the first assignment statement would
resolve to

myFirstVar=hi

that is, $1 would resolve to a variable name implicitly initialized to "". Make
sure $1 and $2 are quoted like this:

Quote:
>myFirstVar= "  '  $1  '  "
>mySecondVar= "  '  $2  '  "  }

Spaces added for clarity. There should be no spaces within the double quotes on
each line.


Sun, 13 May 2001 03:00:00 GMT  
 passing vars to awk script

Quote:



> >#!/bin/ksh
> >awk '
> >BEGIN {
> >myFirstVar='$1'
> >mySecondVar='$2'}
> >{
> ># program goes here using myFirstVar and mySecondVar variables
> >}'

> This works if $1 and $2 are representations of numbers, but not if they're
> text. For example, if $1 were "hi", the first assignment statement would
> resolve to

> myFirstVar=hi

> that is, $1 would resolve to a variable name implicitly initialized to "". Make
> sure $1 and $2 are quoted like this:

> >myFirstVar= "  '  $1  '  "
> >mySecondVar= "  '  $2  '  "  }

Of course. My slip.

Quote:
> Spaces added for clarity. There should be no spaces within the double quotes on
> each line.

Cesar
--
Please remove the uppercase characters from my e-mail address for the
real thing


Sun, 13 May 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing and getting vars. from C to awk

2. HELP: Passing Unix vars to AWK

3. Shell vars into Awk executable script not so easy

4. passing vars to shell script

5. Newbie - passing variable number of vars to expect script

6. Passing command line variables to AWK in shell script

7. passing arguments from awk to another script

8. Passing values outside AWK script

9. passing variables to awk scripts

10. passing arguments into awk script

11. Help passing command line parameters to awk scripts

12. THANKS: Help passing command line parameters to awk script

 

 
Powered by phpBB® Forum Software