if you need a plain ksh example of a utility script, you could do the following:-
Code:
#!/bin/ksh
#timediff - A simple time difference routine
#Requires input of two times (format hh:mm:ss) on command line or will prompt.
#
#Author:- Robin
if [ $# -eq 2 -o $# -eq 3 ]
then
stime=$1 # Start time
etime=$2 # End time
else
read stime?"Please enter the start time: "
read etime?"Please enter the end time: "
read response?"Respond in hh:mm:ss or seconds? (h/S)"
typeset -l response
if [ "$response" = "h" ]
then
set - "" "" hh:mm:ss # Force parm 3 to be hh:mm:ss for later
fi
echo "The difference is: \c" # Write a tag just for interactive use
fi
echo $stime|tr ":" " "|read shh smm sss # Split the time up
echo $etime|tr ":" " "|read ehh emm ess
((shhs=$shh*3600))
((ehhs=$ehh*3600))
((smms=$smm*60))
((emms=$emm*60))
((ssecs=$shhs+$smms+$sss))
((esecs=$ehhs+$emms+$ess))
((delta=$esecs-$ssecs))
if [ $delta -lt 0 ] # Is difference negative, i.e. we're past midnight
then
((delta=$delta+86400)) # Add a day's worth of seconds
fi
if [ "$3" = "hh:mm:ss" ] # is parm 3 set to request formatted output
then
typeset -Z2 dhh dmm dss
((dhh=$delta/3600)) # Use integer match to get whole hours
((delta=$delta-(3600*$dhh))) # Take off enough seconds to compensate
((dmm=$delta/60)) # use integer matsh to get whole minutes
((dss=$delta-(60*$dmm))) # Take off enough seconds to compensate
echo "$dhh:$dmm:$dss"
else
echo $delta # Show total seconds difference
fi
It takes either zero, two or three parameters. With zero, it prompts. With two, it gives the difference in seconds. If parameter 3 is hh:mm:ss then it converts it back to time format.
You can then decide how you want your code to call it, for instance a very simple:-
Code:
#!/bin/ksh
preval=""
cat myfile |while read label val
do
if $preval != "" ]
then
echo "$label is after `timediff $preval $val`"
preval=$val
fi
done
You could, of course add the 3rd parameter to the call to timediff of hh:mm:ss amking the line:
Code:
echo "$label is after `timediff $preval $val hh:mm:ss`"
As a warning, I have just thrown this together, so I haven't worried about considering crossing over midnight more than once in a single step.
I hope that this helps, but do let us know if I have missed the point.
Robin
Liverpool/Blackburn
UK
Last edited by rbatte1; 03-11-2011 at 10:22 AM..
Reason: Adding comments and a calling loop
Hello Friends,
I want to write a script for the following:
nlscux62:tibprod> grep "2008 Apr 30 01:" SA_EHV_SPEED_SFC_IN_03-SA_EHV_SPEED_SFC_IN_03-2.log | grep -i post | more
2008 Apr 30 01:01:23:928 GMT +2 SAPAdapter.SA_EHV_SPEED_SFC_IN_03-SA_EHV_SPEED_SFC_IN_03-2 Info AER3-000095 IDOC... (2 Replies)
Hi guru's,
Am new to shell scripting.
I am getting the below o/p from the oracle database, when I fire a query.
ID JOB_ID ELAPSED_TIME FROM TO
----- ------ ------------------- -------- --------
62663 11773 01/06/2009 09:49:13 SA CM
62664 11773 ... (4 Replies)
Hi,
I have a file with one column data (sample below) and I am trying to write a shell script to calculate the difference between consecutive data valuse i.e
Var = Ni -N(i-1)
0.3141
-3.6595
0.9171
5.2001
3.5331
3.7022
-6.1087
-5.1039
-9.8144
1.6516
-2.725
3.982
7.769
8.88 (5 Replies)
Hi guys
I am deleting a unique line from the file and also need to remove the line above it which is NOT unique and servers as a record separator. Here is an example:
#
101 803E 823F 8240
#
102 755f 4F2A 4F2B
#
290 747D 0926 0927
#
999 8123 813E ... (5 Replies)
I have a file wich contains time formats and i need to get the time difference
TIME1 TIME2
==================================
20120624192555.6Z 20120624204006.5Z
which means first date 2012/6/24 19:25:55,second date 2012/6/24 20:40:06 so when i get the time... (1 Reply)
I have a file wich contains time formats and i need to get the time difference
TIME1 TIME2
=============== ===================
20120624192555.6Z 20120624204006.5Z
which means first date 2012/6/24 19:25:55,second date 2012/6/24 20:40:06 so when i get the time... (23 Replies)
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)
I was looking at this script which outputs the two lines which differs less than one sec.
#!/usr/bin/perl -w
use strict;
use warnings;
use Time::Local;
use constant SEC_MILIC => 1000;
my $file='infile';
## Open for reading argument file.
open my $fh, "<", $file or die "Cannot... (1 Reply)
I have a text file with many thousands of lines, a small sample of which looks like this:
InputFile:PS002,003 D -1 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0
PS002,003 PSQ 0 1 7 18 1 0 -1 1 1 3 -1 -1 ... (5 Replies)