Calculate difference between two successive values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate difference between two successive values
# 1  
Old 10-31-2010
Calculate difference between two successive values

Hi,

I have a file containing timestamps (at micro-seconds granularity). It looks like the following:

06:49:42.383818
06:49:42.390190
06:49:42.392308
06:49:42.392712
06:49:42.393437
06:49:42.393960
06:49:42.402115

Now I need a sed/awk script to take the difference of two successive time values i.e 2nd value - 1st value, 3rd value - 2nd value , 4th - 3rd and so on....and print them in a file.

So if the input file contains 7 entries, the output file should have 6 entries.

Since the input file is quite big (large number of entries), I need sed/awk script to process it quickly.

Any help will be highly appreciated.

Thanks !
# 2  
Old 10-31-2010
There's no check for a clock roll-over, but this might get you started:

Code:
 awk '
        {
                split( $1, a, ":" )
                t = a[1] * 3600;
                t += a[2] * 60;
                t += a[3];

                if( NR > 1 )
                        printf( "%.3f\n", t - last);

                last = t;
        }
'  <input-file

This User Gave Thanks to agama For This Post:
# 3  
Old 10-31-2010
This is a one-liner version of agama's approach which may speed thing up a little bit.
Code:
awk -F: '{t=$1*3600+$2*60+$3} NR>1{printf( "%.6f\n", t-p)}{p=t}' infile

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Calculate time difference

I have time in a file in HH:MM:SS format as it contents(its not the file creation time). i need this to be converted to epoch time or time since 1970. The time is written into that file by a script, which i cannot modify. Im using AIX machine $ cat abc.txt 10:29:34 (2 Replies)
Discussion started by: gpk_newbie
2 Replies

3. Shell Programming and Scripting

Calculate date difference

Hi All Can you please help me with UNIX script code that will work with ksh shell on UNIX server My Requirement Time1: 09/17/13101536 Time2: 09/16/13101536 I want to calculate the difference in minutes for the dates with format given above. I have a requirement to wait for... (5 Replies)
Discussion started by: jimmyb
5 Replies

4. Shell Programming and Scripting

How to calculate difference:?

Experts, file1 : Want to find the difference of $3 field from next line's 3rd field, The difference to be calculated from next lines 3rd field, to current lines lines 3rd field. file1 : Jun24_2013.06242013 3301244928 3133059904 167370640 95% Jun25_1124.06252013 3301244928... (4 Replies)
Discussion started by: rveri
4 Replies

5. Shell Programming and Scripting

Transpose timestamp based on column values and calculate time difference

Hello Expert, I need to transpose Date-Timestamp based on same column values and calculate time difference. The input file would be as below and required output is mentioned in the bottom INPUT File ======== 08/23/2012 12:36:09 JOB_5340 08/23/2012 12:36:14 JOB_5340 08/23/2012... (2 Replies)
Discussion started by: asnandhakumar
2 Replies

6. Shell Programming and Scripting

Adding successive values in awk

Hi, I have a large log file in the following format. Epoch-Time Bytes 899726401 20 899726401 30 899726402 40 899726402 10 899726402 50 899726403 50 899726403 ... (8 Replies)
Discussion started by: sajal.bhatia
8 Replies

7. Shell Programming and Scripting

Difference between corresponding elements of successive rows

Hi, I have a file in the following format a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3 a4 b4 c4 d4 I need a script to find the difference between corresponding values of successive rows. So the output would have one less row than the input file and should look like: a2-a1 b2-b1 c2-c1 d2-d1... (4 Replies)
Discussion started by: sajal.bhatia
4 Replies

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

9. Shell Programming and Scripting

How to calculate the time difference.

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE} The output comes as below: 2008-09-30 06.00.01... (2 Replies)
Discussion started by: suri.tyson
2 Replies

10. Shell Programming and Scripting

How to calculate the time difference...

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. ssh -l ora${sid} ${primaryhost} "tail -2 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" |head -1 | awk '{print echo "PREVIOUS:-- Start Date&Time: " $3,$4,echo "|| End Date&Time:... (1 Reply)
Discussion started by: suri.tyson
1 Replies
Login or Register to Ask a Question