Operating System - HP-UX
1837033 Members
2910 Online
110111 Solutions
New Discussion

Re: script variables within awk

 
SOLVED
Go to solution
Sylvain CROUET
Respected Contributor

script variables within awk

Hi!

I use variables within a script and I would like to use them within an awk command without the -v option.
For example:
THRESHOLD1=90
THRESHOLD2=95
bdf | awk '{print $1, ":$THRESHOLD1:$THRESHOLD2"}' > FILE

I know this would work:
bdf | awk -v thr1=$THRESHOLD1 -v thr2=$THRESHOLD2 '{print $1, ":thr1:thr2"}' > FILE

but I am looking for something... smarter.

Can someone help me?
15 REPLIES 15
ian Dennison
Regular Advisor

Re: script variables within awk

perform a "print" of the line "bdf | awk '{print $1, $THRESHOLD1:$THRESHOLD2"}' " and redirect to a temporary file. Then run that file using the 'sh' command.

This is helpful if the number of variables to ber passed is not known

Alternatively you can string these variables together to pass only 1 variable to the awk statement!

Share and Enjoy! Ian
Lets do it to them before they do it to us! www.fred.net.nz
Sylvain CROUET
Respected Contributor

Re: script variables within awk

I prefer the second solution.
Rodney Hills
Honored Contributor

Re: script variables within awk

Using "-v" is the standard awk mechanism for passing variables. You could use "perl" instead.

bdf | perl -nae 'print "$F[0]:$ENV{THRESHOLD1}:$ENV{THRESHOLD2}' >FILE

HTH

-- Rod Hills
There be dragons...
curt larson_1
Honored Contributor

Re: script variables within awk

depends on which version of awk your running,
if it is a newer version, such as on an 11i system, you can use the ENVIRON array to access the values

export THRESHOLD1=90
export THRESHOLD2=95
bdf | awk '{print "$1, :ENVIRON[THRESHOLD1]:ENVIRON[THRESHOLD2]";}' > FILE


and of course you can also pass values to awk using the command line via argc and argv somewhat like a c program
Sylvain CROUET
Respected Contributor

Re: script variables within awk

Interesting ideas but I do not have very recent servers and perl is not installed on all machines.
curt larson_1
Honored Contributor

Re: script variables within awk

you'll need double qoutes around the variable name

ENVIRON["THRESHOLD1"]

and you don't have to export the variables;
export THRESHOLD1=90
Michael Schulte zur Sur
Honored Contributor

Re: script variables within awk

Hi,

believe Curt, environ is what you need to evaluate envoronment variables from within awk. See man awk.

greetings,

Michael
Sylvain CROUET
Respected Contributor

Re: script variables within awk

Yes, ENVIRON seems to be what I need, but my Solaris machines do not seem to implement a correct version of awk as there is no ENVIRON variable within the awk manual page and it does not work as it does under HP-UX.
Dietmar Konermann
Honored Contributor

Re: script variables within awk

On Solaris you may try /usr/xpg4/bin/awk instead.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Michael Schulte zur Sur
Honored Contributor

Re: script variables within awk

Hi,

can you post an example and why it does not work, i mean any error messages?

greetings,

Michael
Jean-Luc Oudart
Honored Contributor

Re: script variables within awk

awk -v would be the solution , I don;t know why you don't want to use it !

Nevertheless, you can run something like :
(echo 90 && echo 95 && bdf) |
awk 'BEGIN{getline; th1=$0; getline; th2=$0; }
{print $1,th1,th2;}'

Regards,
Jean-Luc
fiat lux
Michael Schulte zur Sur
Honored Contributor

Re: script variables within awk

Hi,

there is an alternativ to environ. Store the values of the environment variables into a file and then read them ony by one.

getline < extvarfile; var1=$0;
getline < extvarfile; var2=$0;

greetings

Michael
Sylvain CROUET
Respected Contributor

Re: script variables within awk

I finally used Ian's solution; but Curt's solution, with the precision brougth by Dietmar, works fine too.
James Lynch
Valued Contributor
Solution

Re: script variables within awk

It's all in the quoting. You could do this:

THRESHOLD1=90
THRESHOLD2=95
bdf | awk "{print \$1, \":$THRESHOLD1:$THRESHOLD2\"}" > FILE


JL
Wild turkey surprise? I love wild turkey surprise!
Rory R Hammond
Trusted Contributor

Re: script variables within awk

I am going to show two other suggestions that didn't show up in the thread.

THRESHOLD1=90
THRESHOLD2=95
bdf | awk '{print $1, ":"THRESHOLD1":"THRESHOLD2}' THRESHOLD1=$THRESHOLD1 THRESHOLD2=$THRESHOLD2 > FILE

or

bdf | awk '{print $1, ":"THRESHOLD1":"THRESHOLD2}' THRESHOLD1=90 THRESHOLD2=95 > FILE

Rory

There are a 100 ways to do things and 97 of them are right