Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

ksh variables inside awk script inside ksh script ?

SOLVED
Go to Solution
Trusted Contributor Trusted Contributor

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?
6 REPLIES
Honored Contributor Honored Contributor

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...
Honored Contributor Honored Contributor

Re: ksh variables inside awk script inside ksh script ?

awk -v

Anil
There is no substitute to HARDWORK
Acclaimed Contributor Acclaimed Contributor

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.
Honored Contributor Honored Contributor

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!
Trusted Contributor Trusted Contributor

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
Highlighted
Trusted Contributor Trusted Contributor

Re: ksh variables inside awk script inside ksh script ?

awk -v Size=$Size '
$NF == Size {Count = Count +1 ...