Using Parameter in Awk 
Author Message
 Using Parameter in Awk

I am new to awk so, please bear with me. My question is if I have a script
which calls awk, and pass a parameter on invocation, how do I use that
parameter variable within awk? For instance, if my script passes a parameter,
$1, and my call to awk within the script is:

awk '{ print $1 }' myfile

How do I get awk to recognize that $1 is the parameter I passsed rather than
the first field in myfile? Your help is appreciated. Thanks.

-



Sat, 31 Mar 2001 03:00:00 GMT  
 Using Parameter in Awk

Quote:
>I am new to awk so, please bear with me. My question is if I have a script
>which calls awk, and pass a parameter on invocation, how do I use that
>parameter variable within awk? For instance, if my script passes a parameter,
>$1, and my call to awk within the script is:

>awk '{ print $1 }' myfile

This is one of the simpler ways,
awk '{ print a }' a=hello myfile

Some versions of awk may need to be written as
awk '{ print a }' -v a=hello myfile

--
Steve
^^^^^ Use these 5 characters instead of bit-bucket to post



Sat, 31 Mar 2001 03:00:00 GMT  
 Using Parameter in Awk

% I am new to awk so, please bear with me. My question is if I have a script
% which calls awk, and pass a parameter on invocation, how do I use that

Note that this is really a shell question, rather than an awk question.
You want the shell to expand its parameter before awk starts.

% awk '{ print $1 }' myfile

single quotes prevent expansion in Bourne-style shells. Double-quotes allow
expansion, so you might try
 awk "{ print $1 }" myfile

However this probably doesn't do what you want. For instance, if the value
of $1 is `bob', what awk sees is
 { print bob }
which evaluates the awk variable bob, which will be nothing. So, you can put
the double quotes needed by the print statement:
 awk "{ print \"$1\" }" myfile
so that awk sees
 { print "bob" }

I've had to quote the double-quotes to get the shell to leave them alone.
You might say this is too complicated, and go back to single quotes:
 awk '{ print "'$1'" }' myfile

To me, this is still complicated, and there's another problem. If $1
evaluates to `"bob"', then what awk sees is
 { print ""bob"" }
which turns out to also print nothing. If $1 evaluates to `"bo', then you
get a syntax error from awk.

You could set an awk variable on the command line
 awk -v bob="$1" '{ print bob }' myfile
or
 awk '{ print bob }' bob="$1" myfile

--

Patrick TJ McPhee
East York  Canada



Sun, 01 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. language rules question: using a (passed in) parameter to define a parameter

2. Outputting info from awk and using the info to name the awk output file

3. shell-parameter for outputfile in awk

4. passing parameter from awk back to korn shell

5. Can AWK recept parameter dynamically?

6. Can AWK recept parameter dynamically?

7. ext. command with awk variable as parameter

8. Pass an AWK variable to a pipeline program as a parameter

9. Help passing command line parameters to awk scripts

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

11. Interpret awk parameter as a command

12. C4 - Fourth parameter using Create

 

 
Powered by phpBB® Forum Software