Script to display time difference..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to display time difference..
# 1  
Old 07-29-2008
Data 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..
# 2  
Old 07-29-2008
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:
Code:
#! /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:
Code:
#! /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:
Code:
#! /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
Code:
....
start=`perl -e 'print time()'`
....
# do your stuff here
end=`perl -e 'print time()`
echo "Total elapsed time in seconds: `expr $end - $start`"


Last edited by chihung; 07-29-2008 at 05:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date time difference in UNIX shell script

There are 2 dates, Tue Oct 1 13:40:19 2013 Sun Sept 30 10:26:23 2013 I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help. (3 Replies)
Discussion started by: tanmoysays
3 Replies

2. Shell Programming and Scripting

Date / Time difference in shell script

Request ID GMDCOMTM GMDRRSTIME GMDRESTIME <36812986> : : :I want to display the date -time difference in other fields. Above I have given for only 1 record. I want to calculate for all the records. (GMCOMTM - GMDRRSTM) ,(GMDRRSTM-GMDRESTM) and... (5 Replies)
Discussion started by: ghosh_tanmoy
5 Replies

3. Shell Programming and Scripting

Date / Time difference in shell script

================================================================================ Request ID GMDCOM TIME GMDRRS TIME COM-RRS ================================================================================ <36812974> Tue Oct 1 13:32:40 2013 Tue Oct 1 20:36:42 2013... (1 Reply)
Discussion started by: ghosh_tanmoy
1 Replies

4. UNIX for Dummies Questions & Answers

Shell script - getting Time difference using awk

Hi..I have the data in a file like in this format, and I need the output time difference in seconds by using awk command. Start date/time and end date/time given in column 2,3 & 4,5. Please assist how to write shell script. File1.txt JOB1 10/09/2013 17:42:16 10/09/2013 17:43:46 SU 6202685/1... (4 Replies)
Discussion started by: mprithvi
4 Replies

5. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

6. Shell Programming and Scripting

script to display time

hey folks, i am stuc in this problem. You all might help me out. I want to write a BASH script to display time every 15 seconds using %r field descriptor. And want to clear the window each time before displaying time using clear command. Please help me out (3 Replies)
Discussion started by: manojrsb
3 Replies

7. Shell Programming and Scripting

compare the log and display the time difference..

Hi All, I've written a script which reads all the systems backup information and saves it in a log file. #!/bin/ksh export ORACLE_SID=$1 export primaryhost=$2 export sid=`echo $ORACLE_SID| tr ` RESULTFILE=/oracle/PC9/backupstatus_prod.log LOGP=`ssh -o StrictHostKeyChecking=no -l... (1 Reply)
Discussion started by: suri.tyson
1 Replies

8. Shell Programming and Scripting

display time required to complete running script

hi is there any way i can display a countdown time needed to run a script? like load a counter at the beginning of the script with the estimated time and display the counter decrementing till it finishes running the script? (3 Replies)
Discussion started by: npatwardhan
3 Replies

9. Shell Programming and Scripting

calculating the time difference, when the script was executed and the currenent file

Hi, I has created the shell script in HP_UX 11.23 and using the command, echo $(date +%Y%m%d%H%M%S) > $DIR/alert, placing the time of running the script into a file alert. I want to compare the time in the above file alert with the current time.If difference is more than 5 min, then print the... (7 Replies)
Discussion started by: velocitnitin
7 Replies

10. UNIX for Dummies Questions & Answers

Script to display last login date/time?

I was wondering if anyone had a script that would display the last time a user logged into a particular machine. I know about the "last" command, but it gives too much info.... I just wanted to know the last time a user used his/her id. ANy help would be greatly appreciated. Ryan (3 Replies)
Discussion started by: ryaneverett5
3 Replies
Login or Register to Ask a Question