Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create loop for fields

Status
Not open for further replies.

toniceam

Programmer
Jun 17, 2005
3
ES
Hi everybody:
I just create awk script. This script calculate the average of a field, but i want to do it for every fields.
The original field has 40 fields of numbers like that:

38.00 51.00 10.00 -99.90 75.00 47.00 4.00 -99.90 69.00 121.00 62.00 6.00 70.00 43.00 36.00 49.00 8.00 36.00 50.00 74.00 125.00 41.00 70.00 39.00 24.00 33.00 53.00 -99.90 70.00 2.00 24.00 71.00 28.00 122.00 31.00 -99.90 40.00 39.00 28.00 94.00 06/26/2005 00:10 (this is only one row, this is field $0)

and the script is:

BEGIN{ FS=" "; sum=0; iFinal=6;}
{
(*)
{
if (NR<=iFinal)
{
sum=sum + $1;
if (NR==iFinal)
{
average=sum/6;
printf "%.4s\n", average;
}
}
else
{
iFinal=NR +5;
sum= $1;
}
}
}

I tried to do this with "for" like this:

for (j=1; j<=40; j++) # put it on (*)

But do not function correctly. Could anybody help how do it?.
Thanks in advance.
 
Something like this ?
for(j=1;j<=40;++j)tsum[j]+=$j

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV for your reply:
But when i do it, i have this result:
On script:

BEGIN{ FS=" "; sum=0; iFinal=6;}

{
for(j=1;j<=40;++j)tsum[j]+=$j
{
if (NR<=iFinal)
{
sum=sum + $j;
if (NR==iFinal)
{
average=sum/6;
printf "%.4s\n", average;
}
}
else
{
iFinal=NR +5;
sum= $j;
}
}
}

And when i execute to tje file, i've that ( and that is not good):

6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6


What can you tell about this?
 
A starting point:
nawk '
{ for(j=1;j<=40;++j) tsum[j]+=$j }
END { for(j=1;j<=40;++j) print tsum[j]/NR }
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top