Calculating the epoch time from standard time using awk and calculating the duration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculating the epoch time from standard time using awk and calculating the duration
# 1  
Old 05-23-2012
Calculating the epoch time from standard time using awk and calculating the duration

Hi All,

I have the following time stamp data in 2 columns
Code:
     Date    TimeStamp(also with milliseconds)
05/23/2012 08:30:11.250
05/23/2012 08:30:15.500
05/23/2012 08:31.15.500

.
.
etc

From this data I need the following output.

Code:
0.00( row1-row1 in seconds)
04.25( row2-row1 in seconds)
64.25(row3-row1 in seconds)

I tried to convert the second column into seconds using HH*60*60+MM*60+SS+MS(milliseconds) formula and subtracting line by line but the problem is if the date changes at some row, I don't know how to proceed.
May be converting ecah column into epoch time and adding together and subtracting row wise can work,
but i don't know how to do that.

Any help in this regard is highly helpful for me.
Thanks
Sidda
# 2  
Old 05-23-2012
Code:
[root@host dir]# cat input
Date TimeStamp
05/23/2012 08:30:11.250
05/23/2012 08:30:15.500
05/23/2012 08:31:15.500
[root@host dir]# cat test.sh
#! /bin/bash

line=1
while IFS=. read x y
do
    if [ $line -eq 1 ]
    then
        (( line++ ))
        continue
    elif [ $line -eq 2 ]
    then
        refTime=$((($(date -d "$x" +%s) * 1000) + $y))
        echo "0.000"
        (( line++ ))
    else
        time=$((($(date -d "$x" +%s) * 1000) + $y))
        echo "scale=3; ($time - $refTime) / 1000" | bc -l
        (( line++ ))
    fi
done < input
[root@host dir]# ./test.sh
0.000
4.250
64.250
[root@host dir]#

Of course, you need GNU date and the bench calculator.
# 3  
Old 05-23-2012
ksh93 version u or newer:
Code:
#!/bin/ksh
{ 
  stamp=""
  read;
  while IFS=. read dd ms
  do
    stamp="$(printf "%(%s)T" "$dd").$ms"
    if [ -z $ref ]; then
      ref=$stamp
    fi
    printf "%0.2f\n" $(( stamp - ref ))
  done
} < infile

There is a typo on the last line of the sample... (05/23/2012 08:31:15.500)
# 4  
Old 05-23-2012
gawk
Code:
$echo "05/23/2012 08:30:11.250
05/23/2012 08:30:15.500
05/23/2012 08:31:15.500" | 
awk -v FS="[/:. ]+" '{MICRO=(mktime($3" "$1" "$2" "$4" "$5" "$6)*1000)+$7; 
if (NR==1)
    {FIRST=MICRO; print FIRST-MICRO} 
else 
    {printf "%.2f\n", (MICRO-FIRST)/1000} }'
0
4.25
64.25

This User Gave Thanks to ni2 For This Post:
# 5  
Old 05-25-2012
Thanks.

Thanks Ni2 and Balajesuri,

Your scripts worked well with little output formatting issues.

Just FYI, I came up with the below command written myself.

Code:
awk '{sub("/"," ",$1);print}' input |awk '{sub("/"," ",$2);print}' | sed -e's/:/ /g' | awk '{printf "%.3f\n", $1*2629743 + $2*86400 + ($3-1970)*31556926 +$4*3600 +$5*60+$6}' |awk 'FNR==1 {p=$1} {print $1-p}'

I simply converted the date and time into epoch time and calculated the elapsed time.

Which worked really well and fulfilled my requirements.

Regards
Sidda
# 6  
Old 05-25-2012
Although this latter approach is not a conversion but an approximation and it will show (big) conversion errors around date changes..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculating Time difference Between two Rows in Linux

16:45:51 10051 77845 16:45:51 10051 77845 16:46:52 10051 77846 16:46:53 10051 77846 Match the last PID then subtract second line time with first line. Please help me with any command or script. working in media company on a project OS: RHEl7 tried command: awk 'function... (2 Replies)
Discussion started by: vivekn
2 Replies

2. Shell Programming and Scripting

Calculating the running time

Hi All, I want to run a utility for all the process id that are running for more than 15 mins. I have captured process id's and the time that they were run in a file like below 1st column represnts the process ids and the 2nd one is the Time < 21014 01:00 21099 01:00 24361 01:03 24406... (5 Replies)
Discussion started by: r_t_1601
5 Replies

3. Shell Programming and Scripting

Calculating script run time in Solaris OS

Hi All, I have written script and wanted to know the run time of it in seconds. i used below logic but am not getting the results in second instead getting error. cat pkloader.sh # if you want to calculate the time in milliseconds then use $(date +%s%N) START_TIME=`date +%s` echo... (2 Replies)
Discussion started by: Optimus81
2 Replies

4. Shell Programming and Scripting

Calculating time windows from logfile timestamps

Hey all. I am working on some scripts in bash to perform a variety of functions; there are a variety of steps involved, and they must happen in a specific sequence; what I need help with is a way to calculate some differences in a timestamp in a logfile. One of the steps in the scripts I am... (3 Replies)
Discussion started by: danowar
3 Replies

5. Shell Programming and Scripting

calculating row-wise standard deviation using awk

Hi, I have a file containing 100,000 rows-by-120 columns and I need to compute for the standard deviation for each row. Any idea on how to calculate row-wise standard deviation using awk? My sample data looks like this: input data: 23 35 12 25 16 17 18 19 29 12 12 26 15 14 15 23 12 12... (2 Replies)
Discussion started by: ida1215
2 Replies

6. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

7. UNIX for Dummies Questions & Answers

Calculating the Hours between two time values

Dear Folks, I want to calculate the elapsed hours between two time columns. I am using timestampdiff method for the same. I am able to get the value. But facing an issue of decimal values. For example the elapsed hours between 09:00:00 and 20:30:00 is coming as 11 instead of 11.5. I am using below... (1 Reply)
Discussion started by: dinesh1985
1 Replies

8. Shell Programming and Scripting

Calculating completion time

The date construct in UNIX can be used to calculate when something is finished: date -v+1H displays the time 1 hour from now. I want to use the same construct in a script, but it is leading to error messages: echo "Finished at: " `date -v+$durationH` where $duration is calculated based on input... (3 Replies)
Discussion started by: figaro
3 Replies

9. Shell Programming and Scripting

Calculating delay time - bash

Hi, I am having the following problem. test > hourOfDay=06 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime 180 test > hourOfDay=07 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime 120 test > hourOfDay=08 ; delayTime=$(((9-$hourOfDay)*60)) ; echo $delayTime bash: (9-08: value... (5 Replies)
Discussion started by: jbsimon000
5 Replies

10. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies
Login or Register to Ask a Question