Average calculation based on number of rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average calculation based on number of rows
# 1  
Old 06-10-2010
Average calculation based on number of rows

Dear users,

I need your support, I have a file like this:

Code:
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 6680572.721
 
272134.539 6680572.714
272134.260 6680573.092

272101.962 6680565.280
272101.903 6680565.156
272101.912 6680565.159
272101.912 6680565.156
272101.905 6680565.158
272101.899 6680565.169
272101.899 6680565.161
272101.894 6680565.173
272101.894 6680565.169

And I need to get the first values block before the row without data (then the second values block before the blanck line and so on), and calculate the simple average value for each row, this is, sum all numbers and divide it by the number of values. I did the following bucle to separate the first values until it finds the blank line but doesn't work:

Code:
while read file
do
        file=($list.txt)
        awk '{ print $1, $2 }' file | awk NF | echo ""${line[0]}" "${line[1]""
done

Separately, I used wc -l to count the values but the file name appears (let's say: wc -l list.txt gives 10 list.txt).

I would appreciate your support to solve my problem.

Thanks in advance.

Last edited by vgersh99; 06-10-2010 at 01:26 PM.. Reason: code tags, please!
# 2  
Old 06-10-2010
Can you please explain your need with an example of the output you need.
# 3  
Old 06-10-2010
Yes, the first two value blocks:

Code:
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 6680572.721

272134.539 6680572.714
272134.260 6680573.092

I need to calculate the average of the first column and get one value, so the sum is:
Code:
 
272134.548
272134.545
272134.546
272134.548
272134.545
272134.543
272134.544
272134.543
272134.540
272134.544

= 272134.5446 (2721345.446/10)

The same with the second column, and get one value.

But the point is that I have to do this for each value block (where both blocks are separated by a blank line). I have 20 000 rows, like 500 value blocks. So, for the two blocks showed above, I will get, let's say:

272134.543 6680572.705 (first value block)

272134.546 6680572.713 (second value block)

It is not important how the pair of output values is (ie. comma-separated, etc), it is just important the pair of output values.

Thanks for your answer.

Last edited by vgersh99; 06-10-2010 at 01:28 PM.. Reason: code tags, please!
# 4  
Old 06-10-2010
Code:
BEGIN{RS="";ORS="";}
{na=split($0,a,"\n");
split("",c);
for (i=1;i<=na;i++){
  nb=split(a[i],b);
  for (j=1;j<=nb;j++){
    c[j]+=b[j];
  }
}
for (i=1;i<=nb;i++){
  printf "%.3f ",c[i]/na;
}
print "\n";
}

It is bit ugly, but it has one advantage, being able to produce proper outcome when additional columns of numbers are added to your data file. Run it like that:
Code:
awk -f script.awk yourfile


Last edited by bartus11; 06-10-2010 at 01:36 PM..
# 5  
Old 06-10-2010
Thank you for your answer, but I received the following error message:

awk: listado.awk:6: or (j=1;j<=nb;j++){
awk: listado.awk:6: ^ syntax error
awk: listado.awk:6: fatal: 0 is invalid as number of arguments for or

Thanks again.
# 6  
Old 06-10-2010
wops, it should be "for" Smilie
# 7  
Old 06-10-2010
Try:
Code:
awk '!NF{print s/c;c=0;s=0;next}{s+=$1;c++}END{print s/c}' file


Last edited by Franklin52; 06-10-2010 at 04:00 PM.. Reason: forgot next statement
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with average calculation.

i have a file with 2 columns. i want to calculate the average of column 1 based on the values of column 2. here's how the file looks like. i want to calculate the sums of numbers corresponding to 1 and then calculate the average. same for numbers corresponding to zero. any help with a code would... (1 Reply)
Discussion started by: onerokeyz
1 Replies

2. Shell Programming and Scripting

Average select rows

I have no idea how to even get started with this script. I need to average field 3 for each of the unique identifiers found in field 1. However, I only want to average these rows when field 2 is equal to 1506 - 2000 (note that i replaced the values field 2 for security reasons, but the real... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

3. Shell Programming and Scripting

Split File based on number of rows

Hi I have a requirement, where i will receive multiple files in a folder (say: /fol1/fol2/). There will be at least 14 to 16 files. The size of the files will different, some may be 80GB or 90GB, some may be less than 5 GB (and the size of the files are very unpredictable). But the names of the... (10 Replies)
Discussion started by: kpk_ds
10 Replies

4. UNIX for Dummies Questions & Answers

Sum the rows number based on first field string value

Hi, I have a file like this one h1 4.70650E-04 4.70650E-04 4.70650E-04 h2 1.92912E-04 1.92912E-04 1.92912E-04 h3A 3.10160E-11 2.94562E-11 2.78458E-11 h4 0.00000E+00 0.00000E+00 0.00000E+00 h1 1.18164E-12 2.74150E-12 4.35187E-12 h1 7.60813E-01 7.60813E-01 7.60813E-01... (5 Replies)
Discussion started by: f_o_555
5 Replies

5. 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

6. 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

7. Shell Programming and Scripting

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 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

8. 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

9. Shell Programming and Scripting

Splitting file based on number of rows

Hi, I'm, new to shell scripting, I have a requirement where I have to split an incoming file into separate files each containing a maximum of 3 million rows. For e.g: if my incoming file say In.txt has 8 mn rows then I need to create 3 files, in which two will 3 mn rows and one will contain 2... (2 Replies)
Discussion started by: wahi80
2 Replies

10. UNIX for Dummies Questions & Answers

Average completion time calculation?

I've been trying all night to come up with a script that will take a file that contains job completion times like this as input: 18:30 17:45 16:39 18:01 17:50 ... and figure the Average completion time. I've tried several things, and I just can't seem to get it to figure correctly. I'm... (5 Replies)
Discussion started by: Seawall
5 Replies
Login or Register to Ask a Question