The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
calculate average of multiple line data smacherla HP-UX 4 05-23-2008 01:39 PM
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 04:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 06:57 AM
calculate average of column 2 onthetopo UNIX for Dummies Questions & Answers 0 07-06-2007 05:16 PM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-05-2005 10:10 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-12-2007
Registered User
 

Join Date: Jul 2007
Posts: 8
Use awk to calculate average of column 3

Suppose I have 500 files in a directory and I need to Use awk to calculate average of column 3 for each of the file, how would I do that?
Reply With Quote
Forum Sponsor
  #2  
Old 07-12-2007
Registered User
 

Join Date: Jul 2007
Posts: 8
I think the more appropiate question is it retarded to use awk in this scenario? Is there a better way?
Reply With Quote
  #3  
Old 07-12-2007
Registered User
 

Join Date: May 2007
Posts: 211
Code:
ls -ltr your_directory |awk '{sum+=$5} END { print "Average = ",sum/NR}'
I think awk is more simple for this task
Reply With Quote
  #4  
Old 07-12-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by lorcan View Post
Code:
ls -ltr your_directory |awk '{sum+=$5} END { print "Average = ",sum/NR}'
I think awk is more simple for this task
i think OP is asking for column3 of inside the file.
Reply With Quote
  #5  
Old 07-12-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,619
Maybe something like:
Code:
$ (cd awkdir ; ls | while read filename ; do awk '{sum+=$3} END { print "Average for " FILENAME " = ",sum/NR}' $filename ; done)
Average for one =  6
Average for three =  26
Average for two =  16
$
Reply With Quote
  #6  
Old 07-27-2007
Registered User
 

Join Date: May 2007
Posts: 24
I think NR isn't the right thing to use, suppose for some lines, field 3 is empty, I think somehow you need an emptiness check and average =s/(NR-numemptylines)
Reply With Quote
  #7  
Old 07-27-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
Originally Posted by onthetopo View Post
I think NR isn't the right thing to use, suppose for some lines, field 3 is empty, I think somehow you need an emptiness check and average =s/(NR-numemptylines)
I think that using just NR is the right approach
as empty values would affect the final average

and the OP had not specified anything about that.

But its good to have a different opinion
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0