Re: ksh variables inside awk script inside ksh script ? (281 Views)
Reply
Trusted Contributor
Stuart Abramson
Posts: 278
Registered: ‎07-15-1999
Message 1 of 7 (281 Views)
Accepted Solution

ksh variables inside awk script inside ksh script ?

Consider this ksh script:

SIZES= 3 4325 8632 160000

for Size in $SIZES
do
awk '
$NF == $Size { Count = Count + 1
Total = Total + $NF }
END { TotGB = Total / 1000
printf ("%8d %6d %6d GB \n", $NF, Count, TotGB) }
' $SYMDEV.1
done > $SYMDEV.2

I can't get the $Size inside the awk to substitute into a 3, 4315, etc.

How do i do that?
Please use plain text.
Honored Contributor
Mel Burslan
Posts: 3,212
Registered: ‎08-26-1998
Message 2 of 7 (281 Views)

Re: ksh variables inside awk script inside ksh script ?

not an awk/sed expert here but you seem to be missing a "{" in this whole thing. I am not sure if it is the problem, but if not, try escaping the "$" character in front of variable size by a "\", i.e., replace "$Size" with "\$Size"

Hope this helps...
________________________________
UNIX because I majored in cryptology...
Please use plain text.
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 3 of 7 (281 Views)

Re: ksh variables inside awk script inside ksh script ?

awk -v

Anil
There is no substitute to HARDWORK
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 4 of 7 (281 Views)

Re: ksh variables inside awk script inside ksh script ?

You are expecting the shell to instantiate variables within single quotes --- that ain't gonna happen but what you could do is something like this:
awk -v Size=${Size} '$NF == Size ....'

The -v Size=${SIZE} create an awk variable 'Size' (not $Size inside awk) and gives it the value of the shell variable ${Size}.

I've intentionally not done everything for you.
If it ain't broke, I can fix that.
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 5 of 7 (281 Views)

Re: ksh variables inside awk script inside ksh script ?

Stuart,

You need to replace the single ticks with double quotes in your awk script otherwise variable expansion will not occur. Also change the double quotes of your printf statement to single quotes so that awk can correctly parse your statements.

cheers!
Please use plain text.
Trusted Contributor
Eknath
Posts: 129
Registered: ‎04-03-2005
Message 6 of 7 (281 Views)

Re: ksh variables inside awk script inside ksh script ?

Hi Stuart,

Try this with -v option


SIZES= 3 4325 8632 160000

for Size in $SIZES
do
awk -v NF=$Size '
{ Count = Count + 1
Total = Total + $NF }
END { TotGB = Total / 1000
printf ("%8d %6d %6d GB \n", $NF, Count, TotGB) }
' $SYMDEV.1
done > $SYMDEV.2

Should work

Cheers !!!
eknath
Please use plain text.
Trusted Contributor
Stuart Abramson
Posts: 278
Registered: ‎07-15-1999
Message 7 of 7 (281 Views)

Re: ksh variables inside awk script inside ksh script ?

awk -v Size=$Size '
$NF == Size {Count = Count +1 ...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation