Script to display time difference..

Hi i've written a script which reads last two line of the log file from N number of servers and send the mail by redirecting to a particular log file.

And the two lines is displayed below.

Oracle Q03 Begin Hot BACKUP Time: 07/23/08 18:35:46
Oracle Q03 End Hot BACKUP Time: 07/24/08 14:18:15

Now i want to calculate the time taken from Begin Hot BACKUP to End Hot BACKUP and display the time difference. ex: Time taken in Hours/Minutes to finish the backup.

Thanks in advance..

It is extremely hard to do it in pure unix shell script.
I came up with my version in perl, python, tcl. I am sure one of these commands is available in your os. IMHO, Tcl is a lot easier to extract timestamp string.

Perl:

#! /usr/bin/perl

use Time::Local;

while (<>) {
        if ( /(\d\d)\/(\d\d)\/(\d\d) (\d\d):(\d\d):(\d\d)$/ ) {
                $yr=$3+2000; $mth=$1; $day=$2;
                $hh=$4; $mm=$5; $ss=$6;

                if (/Begin Hot BACKUP/) {
                        $t0=timelocal($ss,$mm,$hh,$day,$mth,$year);
                }
                if (/End Hot BACKUP/) {
                        $t1=timelocal($ss,$mm,$hh,$day,$mth,$year);
                }
        }
}
print $t1-$t0,"\n";

Python:

#! /usr/bin/python

import sys
import re
import datetime
import time

for newline in sys.stdin.readlines():
        line=newline.rstrip()
        t=re.findall('(\d\d)/(\d\d)/(\d\d) (\d\d):(\d\d):(\d\d)$',line)
        ts=t[0]
        if t:
                if 'Begin Hot BACKUP' in line:
                        _t=datetime.datetime(int(ts[2])+2000, int(ts[0]), int(ts[1]), int(ts[3]), int(ts[4]), int(ts[5]))
                        t0=int(time.mktime(_t.timetuple()))
                if 'End Hot BACKUP' in line:
                        _t=datetime.datetime(int(ts[2])+2000, int(ts[0]), int(ts[1]), int(ts[3]), int(ts[4]), int(ts[5]))
                        t1=int(time.mktime(_t.timetuple()))

print t1-t0

Tcl:

#! /usr/bin/tclsh

while { [gets stdin line] >= 0 } {
        if { [regexp {(\d\d/\d\d/\d\d \d\d:\d\d:\d\d)$} $line x ts] } {
                if { [string match {*Begin Hot BACKUP*} $line] } {
                        set t0 [clock scan $ts]
                }
                if { [string match {*End Hot BACKUP*} $line] } {
                        set t1 [clock scan $ts]
                }
        }
}
puts [expr $t1-$t0]

It would be better if you can 'plant' this in your script

....
start=`perl -e 'print time()'`
....
# do your stuff here
end=`perl -e 'print time()`
echo "Total elapsed time in seconds: `expr $end - $start`"