Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

ksh variables inside awk script inside ksh script ?

SOLVED
Go to solution
Stuart Abramson
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
Mel Burslan
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...
RAC_1
Honored Contributor

Re: ksh variables inside awk script inside ksh script ?

awk -v

Anil
There is no substitute to HARDWORK
Highlighted
A. Clay Stephenson
Acclaimed Contributor
Solution

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.
Sandman!
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!
Eknath
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
Stuart Abramson
Trusted Contributor

Re: ksh variables inside awk script inside ksh script ?

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