The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
trying to create a script to read a flie and loop a command... Italy87 UNIX Desktop for Dummies Questions & Answers 5 03-26-2008 08:36 AM
trying to create a script to read a flie and loop a command... Italy87 UNIX for Advanced & Expert Users 1 03-25-2008 09:44 AM
For loop to create 9000 users xeniya Shell Programming and Scripting 2 05-25-2007 09:48 PM
Create a calculated field from existing fields atchleykl UNIX for Dummies Questions & Answers 1 04-19-2007 12:13 PM
need for loop to include fields as one line prkfriryce Shell Programming and Scripting 8 03-23-2007 12:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-07-2005
tonet
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Thumbs down 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.
  #2 (permalink)  
Old 07-07-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
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")
}

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:24 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0