Calculate average time using a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate average time using a script
# 1  
Old 05-26-2010
Calculate average time using a script

Hello,

I'm hoping to get some help on calculating an average time from a list of times (hour:minute:second).

Here's what my list looks like right now, it will grow (I can get the full date or change the formatting of this as well):

Code:
07:55:31
09:42:00
08:09:02
09:15:23
09:27:45
09:49:26
10:30:41
10:03:51
10:25:14
07:12:30
08:18:31
10:33:37
11:12:20

I'm guessing I can use awk or something, but I'm not quite sure how to get it to work.

Any help is appreciated.

Thanks,
Jared

Last edited by Scott; 05-26-2010 at 05:12 PM.. Reason: Please use code tags
# 2  
Old 05-26-2010
Can you change the format to simply the number of seconds total duration? If not, I would just convert each line to seconds and use that as to generate your average. Convert the average back to whatever format you want to read.
# 3  
Old 05-26-2010
Code:
awk -F':' 'BEGIN{total=0;} {total+=(($1*3600)+($2*60)+$3);} END{printf "%10d\n",(total/NR)}' Edit7

# 4  
Old 05-26-2010
Quote:
Originally Posted by curleb
Code:
awk -F':' 'BEGIN{total=0;} {total+=(($1*3600)+($2*60)+$3);} END{printf "%10d\n",(total/NR)}' Edit7

Thanks curleb!

What's the easiest way to convert this into hour:minute (or hour:minute:second) format?
# 5  
Old 05-26-2010
I'd say the easiest would be to just reverse the calculation within the END{} statement. Instead of just printing the Average, you'd want to go ahead and put it back onto the horse it rode in on:
Code:
$ awk -F':' 'BEGIN{total=0;} {total+=(($1*3600)+($2*60)+$3);} END{a=(total/NR); printf "%02d:%02d:%02d\n",(a/3600),((a/60)%60),(a%60)}' Edit7

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate the average per block.

My old school way is a one liner. And will search for average from SAR, to get the data receive rate. But, I dont think it is practical or accurate,. Because it calculates to off peak hours. I am planning to change it. My cron runs every 30 mins. When my cron runs, and my time is 14:47pm,, it will... (1 Reply)
Discussion started by: invinzin21
1 Replies

2. Shell Programming and Scripting

Calculate Average time of one column

Hello dears, I have a log file with records like below and want to get a average of one column based on the search of one specific keyword. 2015-02-07 08:15:28 10.102.51.100 10.112.55.101 "kevin.c" POST ... (2 Replies)
Discussion started by: Newman
2 Replies

3. Shell Programming and Scripting

Calculate average for repeated ID within a data

I have an awk script that gives the following output: Average end-to-end transmission delay 2.7 to 5.7 is 0.635392 seconds Average end-to-end transmission delay 2.1 to 5.1 is 0.66272 seconds Average end-to-end transmission delay 2.1 to 5.1 is 0.691712 seconds Average end-to-end transmission... (4 Replies)
Discussion started by: ENG_MOHD
4 Replies

4. Shell Programming and Scripting

Calculate average from CSV file using PERL script

Hi All I have this csv file and I need to calculate the average of FPS. FPS:27.7420, Interval:1314184238772 FPS:25.9798, Interval:1314184242646 FPS:27.4772, Interval:1314184246311 FPS:26.1623, Interval:1314184250159 FPS:26.4515, Interval:1314184253972 FPS:31.5896, Interval:1314184257163... (24 Replies)
Discussion started by: sayachop
24 Replies

5. Shell Programming and Scripting

Calculate Average AWK

I want to calculate the average line by line of some files with several lines on them, the files are identical, just want to average the 3rd columns of those files.:wall: Example file: File 1 001 0.046 0.667267 001 0.047 0.672028 001 0.048 0.656025 001 0.049 ... (2 Replies)
Discussion started by: AriasFco
2 Replies

6. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

7. Shell Programming and Scripting

calculate the average of time series data using AWK

Hi, I have two time series data (below) merged into a file. t1 and t2 are in unit of second I want to calculate the average of V1 every second and count how many times "1" in V2 is occur within a second Input File: t1 V1 t2 V2 10.000000... (5 Replies)
Discussion started by: nica
5 Replies

8. Programming

calculate average

I have a file which is 2 3 4 5 6 6 so i am writing program in C to calculate mean.. #include<stdio.h> #include<string.h> #include <math.h> double CALL mean(int n , double x) main (int argc, char **argv) { char Buf,SEQ; int i; double result = 0; FILE *fp; (3 Replies)
Discussion started by: cdfd123
3 Replies

9. UNIX for Dummies Questions & Answers

calculate average of column 2

Hi I have fakebook.csv as following: F1(current date) F2(popularity) F3(name of book) F4(release date of book) 2006-06-21,6860,"Harry Potter",2006-12-31 2006-06-22,,"Harry Potter",2006-12-31 2006-06-23,7120,"Harry Potter",2006-12-31 2006-06-24,,"Harry Potter",2006-12-31... (0 Replies)
Discussion started by: onthetopo
0 Replies

10. UNIX for Dummies Questions & Answers

Please Help ( Calculate time taken in a script)

Hello, I want to know how to calculate time difference in unix shell script. Time command in unix will not solve my problem because it calculates total time taken by a script. See the examle below. example :- #!/bin/ksh # This is just example # Start the timer Shell commands .. Shell... (9 Replies)
Discussion started by: sanjay92
9 Replies
Login or Register to Ask a Question