Hi,
I want to access a variable outside the awk program. My program is as below:- I can not access the exact value of k (See the last line of the program).
#!/usr/bin/sh
j=10
k=1
#k is declared outside awk
awk '
BEGIN {
i=1;
j1="'"$j"'"
printf("\n ## Value of j1 is %d ##", j1);
while (i<=j1)
{
k=i*i;
printf("\n The square of %d is %d", i, k);
i=i+1;
}
printf("\n");
}
'
#Accessing the variable out side the awk program
echo "The value of k is $k"
The current Output is:
## Value of j1 is 10 ##
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The square of 8 is 64
The square of 9 is 81
The square of 10 is 100
The value of k is 1
At the last line, the value of k should be 100...How its possible..?
Second thing is: In the above program, If I delete the line (or comment it)
"printf("\n ## Value of j1 is %d ##", j1);" , then I am getting the following out put:
The square of 1 is 1
The value of k is 1
Question is: Why the output is not comming as before(ie square of first 10 numbers..)...? If I want to get all squares upto 10 without that printf statement, then what should be done..?