Script to find the average of a given column and also for specified number of rows??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to find the average of a given column and also for specified number of rows??
# 1  
Old 02-02-2009
Script to find the average of a given column and also for specified number of rows??

Hi friends
I have 100 files in my directory. Each file look like this..

Temp1 Temp2 Temp3
MAS 1 2 3
MAS 4 5 6
MAS 7 8 9
Delhi 10 11 12
Delhi 13 14 15
Delhi 16 17 18
Delhi 20 21 19
Mumbai 22 46 56
Mumbai 67 34 54
..................................
.................................. so on.

I need a script which gives gives average of the column2 Temp2 for a given row(s) e.g. Delhi or MAS
I got an awk script some thing like below from this unix forum but it is giving the average of all the rows.

$ (cd awkdir ; ls | while read filename ; do awk '{sum+=$3} END { print "Average for " FILENAME " = ",sum/NR}' $filename ; done)

Can anyone please add the grep " MAS/Delhi/.... so on" command to the existing script so that I will get my desired output...

Many thanks in advance to evryone.. This is my first posting..
# 2  
Old 02-02-2009
Hope this helps:

Code:
awk '/Delhi/ {sum+=$3; divby++} END { print "Average for " FILENAME " = ",sum/divby}' FILENAME=foo foo

For a little flexibility, you can also try this:

Code:
awk '$1==searchme {sum+=$3; divby++} END { print "Average for " FILENAME " = ",sum/divby}' FILENAME=foo searchme=Delhi foo

# 3  
Old 02-02-2009
Quote:
Originally Posted by ks_reddy
I have 100 files in my directory. Each file look like this..

Temp1 Temp2 Temp3
MAS 1 2 3
MAS 4 5 6
MAS 7 8 9
Delhi 10 11 12
Delhi 13 14 15
Delhi 16 17 18
Delhi 20 21 19
Mumbai 22 46 56
Mumbai 67 34 54
..................................
.................................. so on.

I need a script which gives gives average of the column2 Temp2 for a given row(s) e.g. Delhi or MAS
I got an awk script some thing like below from this unix forum but it is giving the average of all the rows.

$ (cd awkdir ; ls | while read filename ; do awk '{sum+=$3} END { print "Average for " FILENAME " = ",sum/NR}' $filename ; done)

Can anyone please add the grep " MAS/Delhi/.... so on" command to the existing script so that I will get my desired output...

Code:
for filename in *
do
  printf "Average for %s = " "$filename"
  awk '/^MAS/ || /^Delhi/ { ++n; sum += $3 }
        END { print sum / n }
  ' "$filename"
done

This User Gave Thanks to cfajohnson For This Post:
# 4  
Old 02-02-2009
Hi Johnson Thanks for your quick reply...
But there is one correction in my input data..
My data looks like this..

SNO Day CITY Col1 Col2 Col3 ........... So on
1 02/01/09 MAS 1 2 3
2 02/01/09 MAS 4 5 6
3 02/01/09 MAS 7 8 9
4 02/01/09 Delhi 10 11 12
5 02/01/09 Delhi 13 14 15
6 02/01/09 Delhi 16 17 18
7 02/01/09 Delhi 20 21 19
8 02/01/09 Mumbai 22 46 56
9 02/01/09 Mumbai 67 34 54
......................................................................... an so on.

I need to find the average of column 6(for example, there are around 40 columns in my files) for only the rows which contains Delhi in a given column(in this case it is col3). I hope you understand what I am asking.. It will be a great help for me if you can modified your previous script...
# 5  
Old 02-02-2009
Hi Reddy,
Johnson's script absolutely fine :
for filename in *
do
printf "Average for %s = " "$filename"
awk '/^MAS/ || /^Delhi/ { ++n; sum += $3 }
END { print sum / n }
' "$filename"
done

For the change in your file content just change the line :
awk '/^MAS/ || /^Delhi/ { ++n; sum += $3 }with
awk '/^MAS/ || /^Delhi/ { ++n; sum += $6 } #as u need average of column 6....So that part of the code is dealing with the column number....as u should understand from the Average formula.....Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. UNIX for Dummies Questions & Answers

Writing a script to take the average of two columns every 3 rows

I have a dataset with 120 columns. I would like to write a script, that takes the average of every two columns, starting from columns 2 and 3, and moving consecutively in frames of 3 columns, all the way until the last column. The first column in the output file would be the averages of columns... (1 Reply)
Discussion started by: evelibertine
1 Replies

3. UNIX for Dummies Questions & Answers

Find the average based on similar names in the first column

I have a table, say this: name1 num1 num2 num3 num4 name2 num5 num6 num7 num8 name3 num1 num3 num4 num9 name2 num8 num9 num1 num2 name2 num4 num5 num6 num4 name4 num4 num5 num7 num8 name5 num1 num3 num9 num7 name5 num6 num8 num3 num4 I want a code that will sort my data according... (4 Replies)
Discussion started by: FelipeAd
4 Replies

4. Shell Programming and Scripting

average of rows with same value in the first column

Dear All, I have this file tab delimited A 1 12 22 B 3 34 33 C 55 9 32 A 12 81 71 D 11 1 66 E 455 4 2 B 89 4 3 I would like to make the average every column where the first column is the same, for example, A 6,5 46,5 46,5 B 46,0 19,0 18,0 C 55,0 9,0 32,0 D 11,0 1,0 66,0... (8 Replies)
Discussion started by: paolo.kunder
8 Replies

5. Homework & Coursework Questions

Find the Maximum value and average of a column

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to complete a script which will allow me to find: a) reads a value from the keyboard. (ask the... (4 Replies)
Discussion started by: dstewie
4 Replies

6. UNIX for Dummies Questions & Answers

count number of rows based on other column values

Could anybody help with this? I have input below ..... david,39 david,39 emelie,40 clarissa,22 bob,42 bob,42 tim,32 bob,39 david,38 emelie,47 what i want to do is count how many names there are with different ages, so output would be like this .... david,2 emelie,2 clarissa,1... (3 Replies)
Discussion started by: itsme999
3 Replies

7. Shell Programming and Scripting

Average calculation based on number of rows

Dear users, I need your support, I have a file like this: 272134.548 6680572.715 272134.545 6680572.711 272134.546 6680572.713 272134.548 6680572.706 272134.545 6680572.721 272134.543 6680572.710 272134.544 6680572.715 272134.543 6680572.705 272134.540 6680572.720 272134.544... (10 Replies)
Discussion started by: Gery
10 Replies

8. Shell Programming and Scripting

Script to find the average of a given column and also for specified number of rows?

Hi Friends, In continuation to my earlier post https://www.unix.com/shell-programming-scripting/99166-script-find-average-given-column-also-specified-number-rows.html I am extending my problem as follows. Input: Column1 Column2 MAS 1 MAS 4 ... (2 Replies)
Discussion started by: ks_reddy
2 Replies

9. UNIX for Dummies Questions & Answers

Calculating the Number of Rows and Average

Hi All I like to know how can we calculate the number of rows and the average of the values present in the file. I will not know what will be the rowcount, which will be dynamic in nature of the file. eg. 29 33 48 30 28 (6 Replies)
Discussion started by: pk_eee
6 Replies

10. Shell Programming and Scripting

script to find the average number or files?

Anyone has a script or command in UNIX that can take 4 to five different numbers and calculate the average? (2 Replies)
Discussion started by: bbbngowc
2 Replies
Login or Register to Ask a Question