AWK create loop for fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK create loop for fields
# 1  
Old 07-07-2005
PHP AWK create loop for fields

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. Smilie
tonet
# 2  
Old 07-07-2005
don't quite understand your code, but....
if you want to calculate the average for every field accross all the records/lines, here's some thing to start with. Notice the last two fields on every line are not considered AND also it's assumed that ALL the records/lines have the same number of fields.

Code:
{
  for(i=1; i < NF-2; i++)
    arr[i] += $i

  _nf=i
}
END {
  for(i=1; i<=_nf; i++)
    printf("%.2f%s", arr[i]/NR, (i!=_nf) ? OFS : "\n")
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

3. Shell Programming and Scripting

Bash/awk and for loop to create a template

Source File: google.cz http://czechrepublic.google.com/ http://czechrepublic.google.cz http://czechrepublic.google.com/ http://brno.google.cz http://brno.google.com/ Fail Code root@arisvm ~/g] $ cat trya rm -f ss for i in a.txt do #b=`cat $i|awk '{print $1}'` #c=`cat $i|awk '{print... (4 Replies)
Discussion started by: invinzin21
4 Replies

4. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

5. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

6. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

7. Shell Programming and Scripting

Read all the fields in a Loop

Hi, I have a line like A,B,C,D,E,F and I need to loop for 6 times (as many number of fields in above line) and in every loop I need to get the value into a variable. How can I do it? Pls advise! Thanks much!! (7 Replies)
Discussion started by: prvnrk
7 Replies

8. Shell Programming and Scripting

awk Loop 4 digit fields

Can somebody help me on this script with a loop? gawk --re-interval '{if(($1 ~ /]{4}/) && ($2 ~ /]{4}/) && ($3 ~ /]{4}/)) print $0}' I would like to loop until NF and while $i does not match ($i ~ /]{4}/) anymore then print all matching $i and exit. (5 Replies)
Discussion started by: sdf
5 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. UNIX for Dummies Questions & Answers

Create a calculated field from existing fields

I need to add an average field to a file. I can create it in an on-screen report and it does what I need using the awk command but I can't get it to create a new file with the additional field. Here's what I'm working with: file layout: id:lastname:firstname:grade1:grade2:grade3:grade4 I... (1 Reply)
Discussion started by: atchleykl
1 Replies
Login or Register to Ask a Question