Calculating the average


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculating the average
# 1  
Old 01-25-2007
Calculating the average

This is the cronjob
----------------------

root@a7germ:/home/paxtemp [277] > crontab -l|grep test
57 * * * * /home/paxtemp/test_1.sh
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/paxtemp/test.sh
root@a7germ:/home/paxtemp [278] >


This is the contents of test.sh script
---------------------------------------

root@a7germ:/home/paxtemp [279] > cat test.sh
#!/usr/bin/ksh
#
#Author: Paxley Kekana 01-Nov-2006
#
#set -x
#
export NOW=`date +"%Y%m%d"`
export NOK=`date +"%Y%m%d-%T"`
export NOF=`date +"%H"`
#
echo $NOW
#
cd /var/opt/SURGE/logs
ll -ltr som*|tail -1 > /tmp/pax/a.txt
awk '{print $9}' /tmp/pax/a.txt > /tmp/pax/b.txt
#
dd=$(</tmp/pax/b.txt)
#
grep "Interval No. SURs/Sec =" $dd | tail -1 > /tmp/pax/c.txt
awk '{print ($10*2)}' /tmp/pax/c.txt > /tmp/pax/d.txt
#awk '{print $10}' /tmp/pax/c.txt > /tmp/pax/d.txt
#
ee=$(</tmp/pax/d.txt)
#
#=======================================================#
#
cd ../../DataBroker/logs
ll -ltr sdb.log*|tail -1 > /tmp/pax/a1.txt
awk '{print $9}' /tmp/pax/a1.txt > /tmp/pax/b1.txt
#
ss=$(</tmp/pax/b1.txt)
#
grep sdbApp.cpp $ss | tail -2 |head -1> /tmp/pax/c1.txt #Edited on 14-Nov-2006
#grep "Output Rate :" $ss | tail -1 > /tmp/pax/c1.txt #Commented out on 14-Nov-2006
awk '{print $9}' /tmp/pax/c1.txt > /tmp/pax/d1.txt
#awk '{print ((NF > 9) ? $10 : $9) }' /tmp/pax/c1.txt > /tmp/pax/d1.txt #Commented out 14-Nov-2006
#
ff=$(</tmp/pax/d1.txt)
#
#=========================================================#
#
ll -ltr sdb_*.log* |tail -1 > /tmp/pax/a2.txt
awk '{print $9}' /tmp/pax/a2.txt > /tmp/pax/b2.txt
#
gg=$(</tmp/pax/b2.txt)
#
grep "Output Rate" $gg |tail -1 > /tmp/pax/c2.txt
awk '{print $9}' /tmp/pax/c2.txt > /tmp/pax/d2.txt
#
aa=$(</tmp/pax/d2.txt)
#
#============================================================#
echo
echo
#
#((zz=$ee+$ff+$aa))
#
#echo $zz
echo "scale=1; $aa+$ee+$ff"|bc > /tmp/pax/c3.txt
echo #?
#
zz=$(</tmp/pax/c3.txt)
echo $NOK $zz >> /tmp/pax/results/$NOW.txt
#echo $zz >> /tmp/pax/$NOW.txt
echo $zz >> /tmp/pax/results/$NOF.txt
root@a7germ:/home/paxtemp [280] >

This is the contents of the test_1.sh script
--------------------------------------------

root@a7germ:/home/paxtemp [281] > cat test_1.sh
#!/usr/bin/ksh
#
#Author: Paxley Kekana 20-Nov-2006
#
#set -x
#
export NOW=`date +"%Y%m%d"`
export NOF=`date +"%H"`

#
echo "=======================================" >> /tmp/pax/results/$NOW.txt
#
cd /tmp/pax/results

awk '{s+=$1}END {printf("sum = %f Average = %f\n",s,s/NR)}' $NOF.txt >> /tmp/pax/results/$NOW.txt
echo
#
echo "=======================================" >> /tmp/pax/results/$NOW.txt
#
root@a7germ:/home/paxtemp [282] >

This is the output from one of the output files
-------------------------------------------------

20070123-07:05:01 448.786
20070123-07:10:01 450.359
20070123-07:15:00 450.706
20070123-07:20:00 451.693
20070123-07:25:00 452.346
20070123-07:30:00 452.079
20070123-07:35:00 446.933
20070123-07:40:00 447.399
20070123-07:45:00 447.226
20070123-07:50:00 445.639
20070123-07:55:00 438.506
=======================================
sum = 348791.968000 Average = 454.156208
=======================================
20070123-08:00:01 437.319
20070123-08:05:00 488.893
20070123-08:10:00 486.279
20070123-08:15:00 482.586
20070123-08:20:00 484.853
20070123-08:25:00 483.106
20070123-08:30:00 478.853
20070123-08:35:00 480.053
20070123-08:40:00 481.199
20070123-08:45:00 480.959
20070123-08:50:00 480.413
20070123-08:55:00 483.173
=======================================
sum = 458765.924000 Average = 597.351464
=======================================
20070123-09:00:00 483.746
20070123-09:05:00 642.313
20070123-09:10:00 635.859
20070123-09:15:00 634.059
20070123-09:20:00 633.353
20070123-09:25:01 635.913
20070123-09:30:00 640.899
20070123-09:35:00 639.046
20070123-09:40:01 636.819
20070123-09:45:01 640.686
20070123-09:50:00 639.219
20070123-09:55:00 syntax error on line 1,
=======================================
sum = 550224.286000 Average = 716.437872
=======================================
20070123-10:00:00 syntax error on line 1,
20070123-10:05:01 734.993
20070123-10:10:00 732.713
20070123-10:15:00 731.739
20070123-10:20:00 731.473
20070123-10:25:00 736.793
20070123-10:30:00 739.099
20070123-10:35:00 735.153
20070123-10:40:00 735.486
20070123-10:45:00 736.033
20070123-10:50:00 734.873
20070123-10:55:00 736.899
=======================================
sum = 594294.818000 Average = 773.821378
=======================================
20070123-11:00:00 734.313
20070123-11:05:00 744.179
20070123-11:10:00 743.019
20070123-11:15:00 745.329
20070123-11:20:00 742.389
20070123-11:25:00 741.739
20070123-11:30:00 739.006
20070123-11:35:00 740.339
20070123-11:40:00 741.699
20070123-11:45:01 742.459
20070123-11:50:00 740.726
20070123-11:55:00 739.553
=======================================
sum = 619803.078000 Average = 807.035258
=======================================
20070123-12:00:00 742.673
20070123-12:05:00 756.779
20070123-12:10:00 757.526
20070123-12:15:00 754.753
20070123-12:20:00 755.073
20070123-12:25:00 754.326
20070123-12:30:00 757.886
20070123-12:35:00 760.739
20070123-12:40:00 758.033
20070123-12:45:00 757.593
20070123-12:50:00 756.913
20070123-12:55:00 757.246
=======================================
sum = 621107.320000 Average = 808.733490
=======================================
20070123-13:00:00 755.033
20070123-13:05:00 739.179
20070123-13:10:00 735.699
20070123-13:15:00 738.779
20070123-13:20:00 734.513
20070123-13:25:01 734.899
20070123-13:30:01 735.766
20070123-13:35:00 737.979
20070123-13:40:00 736.606
20070123-13:45:00 733.366
20070123-13:50:00 738.966
20070123-13:55:00 738.406
=======================================
sum = 615483.403000 Average = 801.410681
=======================================
20070123-14:00:00 738.086
20070123-14:05:01 760.233
20070123-14:10:01 766.073
20070123-14:15:01 763.699
20070123-14:20:00 764.513
20070123-14:25:00 762.806
20070123-14:30:00 762.273
20070123-14:35:00 763.019
20070123-14:40:00 766.139
20070123-14:45:00 764.966
20070123-14:50:00 763.939
20070123-14:55:00 765.766
=======================================
sum = 615336.803000 Average = 801.219796
=======================================
20070123-15:00:00 766.233
20070123-15:05:00 762.853
20070123-15:10:00 763.706
20070123-15:15:00 764.239
20070123-15:20:00 760.946
20070123-15:25:00 764.346
20070123-15:30:00 762.973
20070123-15:35:00 764.119
20070123-15:40:00 764.626
20070123-15:45:00 767.946
20070123-15:50:00 762.573
20070123-15:55:01 768.239
=======================================
sum = 628564.810000 Average = 818.443763
=======================================
20070123-16:00:00 770.666
20070123-16:05:01 683.279
20070123-16:10:01 688.453
20070123-16:15:00 686.866
20070123-16:20:01 687.546
20070123-16:25:00 688.973
20070123-16:30:01 695.079
20070123-16:35:00 694.239
20070123-16:40:00 696.333
20070123-16:45:00 699.919
20070123-16:50:00 694.573
20070123-16:55:00 696.319
=======================================
sum = 626349.391000 Average = 815.559103
=======================================
20070123-17:00:00 697.413
20070123-17:05:01 600.046
20070123-17:10:01 601.966
20070123-17:15:00 595.059
20070123-17:20:00 599.099
20070123-17:25:00 594.619
20070123-17:30:00 597.499
20070123-17:35:00 596.273
20070123-17:40:00 595.366
20070123-17:45:00 597.406
20070123-17:50:00 594.233
20070123-17:55:00 592.006
=======================================
sum = 646678.355000 Average = 832.275875
=======================================
20070123-18:00:00 589.326
20070123-18:05:01 633.206
20070123-18:10:01 631.193
20070123-18:15:01 626.113
20070123-18:20:00 625.513
20070123-18:25:00 622.473
20070123-18:30:00 636.646
20070123-18:35:00 628.233
20070123-18:40:01 623.699
20070123-18:45:01 626.499
20070123-18:50:01 618.353
20070123-18:55:00 621.179
=======================================
sum = 655142.155000 Average = 839.925840
=======================================
20070123-19:00:00 615.406
20070123-19:05:00 722.759
20070123-19:10:00 721.986
20070123-19:15:00 718.919
20070123-19:20:01 719.853
20070123-19:25:01 723.426
20070123-19:30:01 724.173
20070123-19:35:00 721.546
20070123-19:40:00 721.706
20070123-19:45:00 716.866
20070123-19:50:00 718.719
20070123-19:55:00 718.199
=======================================
sum = 670887.911000 Average = 860.112706
=======================================
20070123-20:00:00 720.093
20070123-20:05:00 852.459
20070123-20:10:00 843.939
20070123-20:15:00 848.619
20070123-20:20:01 846.459
20070123-20:25:01 842.619
20070123-20:30:01 847.139
20070123-20:35:00 859.486
20070123-20:40:00 853.459
20070123-20:45:00 848.473
20070123-20:50:01 850.593
20070123-20:55:00 851.966
=======================================
sum = 675971.583000 Average = 866.630235
=======================================
20070123-21:00:00 848.793
20070123-21:05:00 863.973
20070123-21:10:00 861.919
20070123-21:15:00 858.599
20070123-21:20:00 847.813
20070123-21:25:00 849.853
20070123-21:30:00 851.226
20070123-21:35:00 847.733
20070123-21:40:00 846.479
20070123-21:45:01 841.399
20070123-21:50:00 840.173
20070123-21:55:00 832.866
=======================================
sum = 637222.257000 Average = 816.951612
=======================================
20070123-22:00:00 830.093
20070123-22:05:01 730.919
20070123-22:10:00 728.106
20070123-22:15:00 724.706
20070123-22:20:00 730.959
20070123-22:25:00 718.933
20070123-22:30:00 713.213
20070123-22:35:00 709.279
20070123-22:40:01 705.746
20070123-22:45:00 697.946
20070123-22:50:01 698.026
20070123-22:55:00 691.066
=======================================
sum = 560332.761000 Average = 718.375335
=======================================
20070123-23:00:00 693.253
20070123-23:05:00 713.526
20070123-23:10:00 709.473
20070123-23:15:00 707.793
20070123-23:20:00 706.459
20070123-23:25:01 701.526
20070123-23:30:00 700.499
20070123-23:35:00 697.903
20070123-23:40:00 697.286
20070123-23:45:00 693.329
20070123-23:50:01 693.933
20070123-23:55:00 691.633
=======================================
sum = 516424.839000 Average = 662.083127
=======================================
root@a7germ:/tmp/pax/results [287] >



====================================================================================
=THE AVERAGE IS NOT RIGHT. PLEASE GIVE YOUR COMMENTS.===============================
====================================================================================
# 2  
Old 01-25-2007
Quote:
Originally Posted by kekanap
awk '{s+=$1}END {printf("sum = %f Average = %f\n",s,s/NR)}' $NOF.txt >> /tmp/pax/results/$NOW.txt

This is the output from one of the output files
-------------------------------------------------

20070123-07:05:01 448.786
20070123-07:10:01 450.359
20070123-07:15:00 450.706
20070123-07:20:00 451.693
20070123-07:25:00 452.346
20070123-07:30:00 452.079
20070123-07:35:00 446.933
20070123-07:40:00 447.399
20070123-07:45:00 447.226
20070123-07:50:00 445.639
20070123-07:55:00 438.506
=======================================
sum = 348791.968000 Average = 454.156208
=======================================
Your files must be out of sync with each other...
Code:
$ cat data
448.786
450.359
450.706
451.693
452.346
452.079
446.933
447.399
447.226
445.639
438.506
$ bc
bc 1.03 (Nov 2, 1994)
Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=6
448.786 + 450.359 + 450.706 + 451.693 + 452.346 + 452.079 + 446.933 + 447.399 + 447.226 + 445.639 + 438.506
4931.672
./11
448.333818
^D$
$
$ awk '{s+=$1}END {printf("sum = %f Average = %f\n",s,s/NR)}' data
sum = 4931.672000 Average = 448.333818
$

Display the contents of that NOF.txt file. I think you will find that the average is correct but you're averaging the wrong stuff.
# 3  
Old 01-26-2007
Honestly, I didn't read the entire script, I will simply post one example that I'm using, hopefully you will get the idea :
Code:
#!/bin/bash

sum=0
count=0

for i in `cat my-time-file | grep { | sed -e "s/{#//" -e "s/#}//"`; do sum="$sum+$i"; count=`expr $count + 1`; done

echo "Entries=$count"
sum=$(echo $sum | bc)
echo "Sum=$sum"
avg=$(echo "scale=2; ($sum) / $count" | bc)
echo "Average=$avg"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculating average from files

I have some files with the following contents.I would like to calculate average of fifth column. How can I do this with awk? file1 cat 95.9 152 78.0 17.9 rat 67.1 153 36.5 30.6 dog 81.4 154 68.1 13.3 dog 92.0 155 55.5 36.5 rat 73.8 156 23.9 49.9 file2 rat... (4 Replies)
Discussion started by: avina
4 Replies

2. Shell Programming and Scripting

Python Script Calculating Average

Can anyone explain what each line of the code does and how it works? I have no experience with python so I am not sure how the arrays and such work. I found this code while looking through the forums. f = open("exams","r") l = f.readline() while l: l = l.split(" ") values = l ... (22 Replies)
Discussion started by: totoro125
22 Replies

3. UNIX for Dummies Questions & Answers

Calculating average

Hi I have file like below 111,victor,48,12,36 342,Peter,54,58,30 476,Scott,25,36,48 567,Patty,74,17,95 I have written below code to calcualte avereage for every id Victor = 48+12+36/3 #!/bin/ksh /usr/xpg4/bin/awk ' BEGIN {FS=","} {sum=0; n=0;i=3 (1 Reply)
Discussion started by: stew
1 Replies

4. Shell Programming and Scripting

Calculating the average of scores

Hi I have 2 files file1 aac 23 25 aac 87 90 aac 33 67 file2 23 0.9 24 0.8 25 0.4 ........ 67 0.55 ........ I want to get output as (11 Replies)
Discussion started by: anurupa777
11 Replies

5. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

6. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

7. Shell Programming and Scripting

Calculating average of 100 different files of same size

Hey guys..... I have many files (lets say 100 or more) of same size, and I want to create a new output file and calculate the average of first row fifth column in all files and print it in first row of output file, then 2nd row fifth col in all 100 files and print it in 2nd row of output... (1 Reply)
Discussion started by: CAch
1 Replies

8. UNIX for Dummies Questions & Answers

Calculating weighted average

Dear all, i have 200 values in a file. How can i calculate a weighted average and output into a new file avg.dat? INPUT: file1.dat 1.3453 2.434 2.345 ..... OUTPUT: avg.dat file1: 1.762 Thanks. Po (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

9. UNIX for Dummies Questions & Answers

Calculating average

Hi, i have 12 float variables in a bash file and i want to calculate the average of them. Can any body help? (6 Replies)
Discussion started by: limadario
6 Replies

10. 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
Login or Register to Ask a Question