![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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?
|
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
I think the more appropiate question is it retarded to use awk in this scenario? Is there a better way?
|
|
#3
|
|||
|
|||
|
Code:
ls -ltr your_directory |awk '{sum+=$5} END { print "Average = ",sum/NR}'
|
|
#4
|
|||
|
|||
|
i think OP is asking for column3 of inside the file.
|
|
#5
|
||||
|
||||
|
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
$
|
|
#6
|
|||
|
|||
|
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)
|
|
#7
|
|||
|
|||
|
Quote:
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 |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|