Operating System - HP-UX
1748038 Members
4987 Online
108757 Solutions
New Discussion юеВ

Script to calculate duration

 
SOLVED
Go to solution
Ahmad Munawwar
Frequent Advisor

Script to calculate duration

Hi,

I have the following date time format:

start_date_time|stop_date_time
Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM

Wanted to get a duration = stop_date_time - start_date_time

Need help on scripting.

reagrds,
Munawar
17 REPLIES 17
Ng Oon Tian
Occasional Advisor

Re: Script to calculate duration

You want to do this in a shell script or do you have something like perl?
Ahmad Munawwar
Frequent Advisor

Re: Script to calculate duration

Hi Ng,

Any would do. I would prefer in shell script.

But if you have one in perl, would great to share.

renarios
Trusted Contributor

Re: Script to calculate duration

Hi Ahmad,

I created someting like that with a while loop that prints every timeout period to screen and kills the process after one hour :

MAX_TIMEOUT=3600 # Maximum backup time in seconds per process (3600 is one hour)
TIMEOUT="60" # Verify period of one minute

while (( $(UNIX95= ps -xC $(basename ${PROG}) |grep ${ORACLE_SID} |wc -w) > 0 ))
do
# wait for the timeout period
sleep ${TIMEOUT}

# update counter with one
(( counter +=1 ))
print counter=${counter}
# calculate total waiting time
(( wait_time=${counter}*${TIMEOUT} ))

# print backup time to screen
print backup ${ORACLE_SID} running ${wait_time} seconds

if (( ${wait_time} >= ${MAX_TIMEOUT} ))
then
print "Killing ${ORACLE_SID} backup process after ${wait_time} seconds"
kill $(UNIX95= ps -xC $(basename ${PROG}) -o pid | tail -1 )
fi
done

Hope it helps,

Renarios
Nothing is more successfull as failure
Ahmad Munawwar
Frequent Advisor

Re: Script to calculate duration

Actually...I have that list of above data in a text file.

I just want to get a duration for each data and write it on next field.

Existing file:

|Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM|

Expected Result:

|Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM||


Ermin Borovac
Honored Contributor
Solution

Re: Script to calculate duration

Here is a quick perl script (it expects perl as /usr/bin/perl). You can run it as

$ ./script.pl
Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM|2067

It prints out duration in seconds (you can redirect output to the new file).

The script disregards 3 digits before PM/AM. I assume this is fraction of millisecond.
Ermin Borovac
Honored Contributor

Re: Script to calculate duration

Error checking is left as an exercise to the reader :-)
Peter Godron
Honored Contributor

Re: Script to calculate duration

Ahmad,
for the solution you should read:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xea948cc5e03fd6118fff0090279cd0f9,00.html
A. Clay Stephenson's script does this and a lot more!

Regards
Ahmad Munawwar
Frequent Advisor

Re: Script to calculate duration

Ok..Guys

I will try now..will let you know soon :-)
Ahmad Munawwar
Frequent Advisor

Re: Script to calculate duration

I got some hick up:

The script confuse on AM and PM cause the duration wrong. For example:

|Jan 5 2005 12:39:48:890AM|Jan 5 2005 1:15:17:173AM|-41071

|Jan 6 2005 11:58:58:303AM|Jan 6 2005 12:16:39:466PM|44261

|Jan 9 2005 12:56:18:443PM|Jan 9 2005 1:30:49:986PM|-41129