Calculatin cumulative elapsed time from mm:ss.ss data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculatin cumulative elapsed time from mm:ss.ss data
# 1  
Old 04-15-2013
Calculatin cumulative elapsed time from mm:ss.ss data

Hello all,

I got some time duration data like below and I want to compute the cumulative elapsed time. The data is in MM:SS.SS format. I got struck with logic on what to do when it changes from 59:59.ss to 00:00.ss.

Code:
59:59.4
59:59.6
59:59.9
00:00.1
00:00.4
00:00.6
00:00.9

I need the below cumulative elapsed time(in seconds) as output
Code:
0
0.2
0.5
0.7
1.0
1.2
1.5

In reality I have millions of rows in my file.
Any help on this is appreciated.
Thanks
Sidda
# 2  
Old 04-15-2013
Code:
awk -F ':'   'FNR==1 {minutes=$1; old=$1; sec=$2; start=(minutes*60) +sec}
                {$old!=$1} {minutes++; old=$1}
                {print  (minutes*60 + $2) - start } ' infile

Start with that.

Last edited by jim mcnamara; 04-15-2013 at 08:20 AM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-15-2013
Code:
$ awk -F ":" 'FNR==1{ti=$1*60+$2}
{s=0}
$1=="00"{s=3600;}
{print ($1*60+s+$2-ti)}' file

0
0.2
0.5
0.7
1
1.2
1.5

This User Gave Thanks to pamu For This Post:
# 4  
Old 04-15-2013
With a million lines of input (assuming adjacent timestamps represent monotonically increasing times [even though only minutes and seconds are given] and assuming that there is at least one entry every hour), the time covered is somewhere between 27 hours and 42,000 days. The script provided by pamu assumes that all timestamps to be processed occur in a single one hour period and prints some elapsed times as negative values).

Jim McNamara said his script was a starting point. But, it looks like he didn't test it with your sample data. (It has an extraneous {$ and } and doesn't check for cases where the minutes are the same as the previous line but the seconds are less than the previous line.)

If my assumptions about your timestamps are correct, I think the following awk script comes closer to doing what you want. However, when the number of seconds reaches 60, it will print MM:SS.S; when the number of elapsed minutes reaches 60, it will print HH:MM:SS.S instead of just HH:SS.S; and when the number of elapsed hours reaches 24, it will print Days:HH:MM:SS.S in addition to the number of elapsed seconds:
Code:
awk -F: '
function tp(seconds,    d, h, m, s) {
        if(seconds < 0) {
                s = seconds + 3600
                h = hours - 1
        } else {
                s = seconds
                h = hours
        }
        m = int(s / 60)
        s = s % 60
        d = int(h / 24)
        h = h % 24
        if(d + h + m) printf("%-20.1f ", ((((d * 24) + h) * 60) + m) * 60 + s)
        if(d) printf("%d:", d)
        if(d + h) printf("%02d:", h)
        if(d + h + m) printf("%02d:", m)
        printf("%04.1f\n", s)
}
NR == 1 {
        hours = 0
        start = (minutes = $1) * 60 + (seconds = $2)
}
{       if(minutes > $1 || (minutes == $1 && seconds > $2)) hours++
        tp((minutes = $1) * 60 + (seconds = $2) - start)
}' data

This can obviously be significantly simplified if you really just want to print the elapsed seconds (without splitting it into days, hours, minutes, and seconds).

As always, if you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-16-2013
Thanks..

Thanks Don, Pamu and Jim,

All the scripts worked well.
Also I forgot to mention the elapsed time in my file with million rows actually varies from few seconds to few hours(based on another context data column values).
So the script provided by Don is really great and helpful in future also in case if I need to calculate elapsed time with days.

Sidda
# 6  
Old 04-17-2013
That's an incredibly good script provided.

Any chance you might want to change the log file format to include "number of seconds since the epoch"? Then you would not have the rollover problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help in : sorting process by their elapsed time: HP-UX

What is the equivalent command of the below linux command would be in hp-ux UNIX95=1 ps -eo pid,start,stime,command Thanks a lot, (1 Reply)
Discussion started by: rveri
1 Replies

2. Shell Programming and Scripting

Compare two timestamps and print elapsed time

Hi, I am unable to Difference between two time stamps in Linux and display the total elapsed time . Source date: Aug 15, 2012 02:00:03 Target date: Aug 14, 2012 18:00:03 # based on the forums I am using the below function. Converted dates into this format Src_dt=20120814180003... (7 Replies)
Discussion started by: onesuri
7 Replies

3. Shell Programming and Scripting

Time elapsed since script started

Hi I want to know if there is anyway I can find out how long it has been since I started my script or total time it has been since my script is executing. Idea here is I want to check if my script is taking more than 30minutes to execute I want to kill that process. Thanks in advance. (1 Reply)
Discussion started by: dashing201
1 Replies

4. Shell Programming and Scripting

elapsed time

Display the elapsed time $ export data_ini = `date +'%s'`; echo $data_ini 1292417961 $ export data_final = `date +'%s'`; echo $data_final 1292418079 $ ((temps = data_final - data_ini)); echo $temps 118 $ echo $((data_final - data_ini)) #total seconds 118 $ echo $(((data_final... (1 Reply)
Discussion started by: aika
1 Replies

5. Shell Programming and Scripting

Elapsed time in seconds in awk

I am trying to get the ellapsed time in seconds in the body of the awk script. I use unix date to get the time. It works in BEGIN {} but not in the body {} of awk. Any ideas? $ cat a BEGIN { "date +%s" | getline x print x } { "date +%s" | getline y print y } $ echo "one line" |... (3 Replies)
Discussion started by: arturas123
3 Replies

6. Shell Programming and Scripting

Help on Time elapsed?

Hi All, I have 2 variables like SDATE and EDATE. Now for example i ll give you values for the above 2 variables. SDATE=11/08/09 11:22 EDATE=11/09/09 22:33 the values of the above variables are represented like this>>>>>> mm/dd/yy hh:mm Now I want to evaluate total time elapsed... (3 Replies)
Discussion started by: smarty86
3 Replies

7. Shell Programming and Scripting

Need to calculate elapsed time

Hi there, How to calculate the elapsed time in minutes for a particular job run under unix. I tried the following $ ps -efo user,pid,etime,comm,args | grep myscript | grep -v grep | awk -F" " '{print $3}' OUTPUT: 01:02:49 I need to get this output in minutes. Can someone help me... (1 Reply)
Discussion started by: karthickrn
1 Replies

8. Shell Programming and Scripting

Calculate Elapsed Time

I'm looking for the cleanest way to calculate the time elapsed between two times in KSH. In minutes or in hours and minutes if it has been longer than 59 minutes. Here are some random examples: Example result: 25 Minutes or Example result: 1 Hour and 25 Minutes Example time format: ... (5 Replies)
Discussion started by: sysera
5 Replies

9. Shell Programming and Scripting

Adding Elapsed time

I'm using the Bourne shell and trying to write a script that will add all the time that any particular user has been on the network for. I've used last-h | grep "username" | cut -c 58-62 to get the times. Then I wrote a script that takes the time and converts it into just minutes. Now I... (1 Reply)
Discussion started by: jrdnoland1
1 Replies

10. Programming

Displaying elapsed time...

I am trying to display the amount of time that it took for a command to run. I'm assuming that i have the correct code: ... else { printf("I am a child process and my pid is %d\n", getpid()); cout<<"Parameters are: "<<endl; for... (5 Replies)
Discussion started by: jj1814
5 Replies
Login or Register to Ask a Question