Time Difference - Comparison

  • Follow


Greetings,

I have a file with following format

Task1  01/12/2010  14:14:04
Task2  01/12/2010  14:00:07
Task3  01/12/2010  14:00:42

I am currently trying to achieve that if the time (last column) for
any of the above task is more than 2 hours (as compared to current
timing), then we need to get notified.

Can anyone advise how to go about it. I am currently using ksh

TIA
0
Reply harpreet.noni (9) 1/12/2010 8:11:36 PM

On Jan 12, 2:11=A0pm, Pankaj <harpreet.n...@gmail.com> wrote:
> Greetings,
>
> I have a file with following format
>
> Task1 =A001/12/2010 =A014:14:04
> Task2 =A001/12/2010 =A014:00:07
> Task3 =A001/12/2010 =A014:00:42
>
> I am currently trying to achieve that if the time (last column) for
> any of the above task is more than 2 hours (as compared to current
> timing), then we need to get notified.
>
> Can anyone advise how to go about it. I am currently using ksh
>
> TIA

This will print the number of hours difference and the task name for
each line in your input file:

gawk '{split($0,t,/[ /:]+/);
print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) /
3600, $1}' file

    Ed.
0
Reply Ed 1/12/2010 8:54:16 PM


On Jan 12, 3:54=A0pm, Ed Morton <mortons...@gmail.com> wrote:
> On Jan 12, 2:11=A0pm, Pankaj <harpreet.n...@gmail.com> wrote:
>
> > Greetings,
>
> > I have a file with following format
>
> > Task1 =A001/12/2010 =A014:14:04
> > Task2 =A001/12/2010 =A014:00:07
> > Task3 =A001/12/2010 =A014:00:42
>
> > I am currently trying to achieve that if the time (last column) for
> > any of the above task is more than 2 hours (as compared to current
> > timing), then we need to get notified.
>
> > Can anyone advise how to go about it. I am currently using ksh
>
> > TIA
>
> This will print the number of hours difference and the task name for
> each line in your input file:
>
> gawk '{split($0,t,/[ /:]+/);
> print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) /
> 3600, $1}' file
>
> =A0 =A0 Ed.

Thanks Ed but it seems I do not have gawk in sh or ksh here. I get
following error

gawk:  not found

Any alternate way to go about it.
0
Reply Pankaj 1/12/2010 9:08:06 PM

On 1/12/2010 3:08 PM, Pankaj wrote:
> On Jan 12, 3:54 pm, Ed Morton<mortons...@gmail.com>  wrote:
>> On Jan 12, 2:11 pm, Pankaj<harpreet.n...@gmail.com>  wrote:
>>
>>> Greetings,
>>
>>> I have a file with following format
>>
>>> Task1  01/12/2010  14:14:04
>>> Task2  01/12/2010  14:00:07
>>> Task3  01/12/2010  14:00:42
>>
>>> I am currently trying to achieve that if the time (last column) for
>>> any of the above task is more than 2 hours (as compared to current
>>> timing), then we need to get notified.
>>
>>> Can anyone advise how to go about it. I am currently using ksh
>>
>>> TIA
>>
>> This will print the number of hours difference and the task name for
>> each line in your input file:
>>
>> gawk '{split($0,t,/[ /:]+/);
>> print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) /
>> 3600, $1}' file
>>
>>      Ed.
>
> Thanks Ed but it seems I do not have gawk in sh or ksh here. I get
> following error
>
> gawk:  not found
>
> Any alternate way to go about it.

Just install gawk (http://www.gnu.org/software/gawk/), you'll thank yourself 
later...

	Ed.
0
Reply Ed 1/13/2010 3:23:45 AM

On Tue, 12 Jan 2010 21:23:45 -0600, Ed Morton wrote:

> On 1/12/2010 3:08 PM, Pankaj wrote:
>> On Jan 12, 3:54 pm, Ed Morton<mortons...@gmail.com>  wrote:
>>> On Jan 12, 2:11 pm, Pankaj<harpreet.n...@gmail.com>  wrote:
>>>
>>>> Greetings,
>>>
>>>> I have a file with following format
>>>
>>>> Task1  01/12/2010  14:14:04
>>>> Task2  01/12/2010  14:00:07
>>>> Task3  01/12/2010  14:00:42
>>>
>>>> I am currently trying to achieve that if the time (last column) for
>>>> any of the above task is more than 2 hours (as compared to current
>>>> timing), then we need to get notified.
>>>
>>>> Can anyone advise how to go about it. I am currently using ksh
>>>
>>>> TIA
>>>
>>> This will print the number of hours difference and the task name for
>>> each line in your input file:
>>>
>>> gawk '{split($0,t,/[ /:]+/);
>>> print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) /
>>> 3600, $1}' file
>>>
>>>      Ed.
>>
>> Thanks Ed but it seems I do not have gawk in sh or ksh here. I get
>> following error
>>
>> gawk:  not found
>>
>> Any alternate way to go about it.
> 
> Just install gawk (http://www.gnu.org/software/gawk/), you'll thank
> yourself later...
> 
> 	Ed.

If you are using a reasonably recent ksh93, then you can ignore Ed's 
advice as ksh93 can do it with builtin commands. The key is to note that

printf "%(%#)T" now

will give you the time in seconds since the epoch, and you can substitute 
any reasonable phrase for "now", like "last monday".

So something like

while	read task day time
do	integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now)
	if	((age > 2*3600))
	then	printf "%s is %d seconds old\n" "$task" $age
	fi
done

should do it.
0
Reply Icarus 1/13/2010 4:36:52 AM

On 1/12/2010 10:36 PM, Icarus Sparry wrote:
> On Tue, 12 Jan 2010 21:23:45 -0600, Ed Morton wrote:
>
>> On 1/12/2010 3:08 PM, Pankaj wrote:
>>> On Jan 12, 3:54 pm, Ed Morton<mortons...@gmail.com>   wrote:
>>>> On Jan 12, 2:11 pm, Pankaj<harpreet.n...@gmail.com>   wrote:
>>>>
>>>>> Greetings,
>>>>
>>>>> I have a file with following format
>>>>
>>>>> Task1  01/12/2010  14:14:04
>>>>> Task2  01/12/2010  14:00:07
>>>>> Task3  01/12/2010  14:00:42
>>>>
>>>>> I am currently trying to achieve that if the time (last column) for
>>>>> any of the above task is more than 2 hours (as compared to current
>>>>> timing), then we need to get notified.
>>>>
>>>>> Can anyone advise how to go about it. I am currently using ksh
>>>>
>>>>> TIA
>>>>
>>>> This will print the number of hours difference and the task name for
>>>> each line in your input file:
>>>>
>>>> gawk '{split($0,t,/[ /:]+/);
>>>> print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) /
>>>> 3600, $1}' file
>>>>
>>>>       Ed.
>>>
>>> Thanks Ed but it seems I do not have gawk in sh or ksh here. I get
>>> following error
>>>
>>> gawk:  not found
>>>
>>> Any alternate way to go about it.
>>
>> Just install gawk (http://www.gnu.org/software/gawk/), you'll thank
>> yourself later...
>>
>> 	Ed.
>
> If you are using a reasonably recent ksh93, then you can ignore Ed's
> advice as ksh93 can do it with builtin commands.

I get where you're coming from, but just because you can apparently do this one 
thing with some constructs specific to some versions of ksh93 doesn't mean it's 
a bad idea to install and use gawk for this and your future text processing 
needs instead.

	Ed.


  The key is to note that
>
> printf "%(%#)T" now
>
> will give you the time in seconds since the epoch, and you can substitute
> any reasonable phrase for "now", like "last monday".
>
> So something like
>
> while	read task day time
> do	integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now)
> 	if	((age>  2*3600))
> 	then	printf "%s is %d seconds old\n" "$task" $age
> 	fi
> done
>
> should do it.

0
Reply Ed 1/13/2010 5:07:36 AM

On Jan 13, 12:07=A0am, Ed Morton <mortons...@gmail.com> wrote:
> On 1/12/2010 10:36 PM, Icarus Sparry wrote:
>
>
>
>
>
> > On Tue, 12 Jan 2010 21:23:45 -0600, Ed Morton wrote:
>
> >> On 1/12/2010 3:08 PM, Pankaj wrote:
> >>> On Jan 12, 3:54 pm, Ed Morton<mortons...@gmail.com> =A0 wrote:
> >>>> On Jan 12, 2:11 pm, Pankaj<harpreet.n...@gmail.com> =A0 wrote:
>
> >>>>> Greetings,
>
> >>>>> I have a file with following format
>
> >>>>> Task1 =A001/12/2010 =A014:14:04
> >>>>> Task2 =A001/12/2010 =A014:00:07
> >>>>> Task3 =A001/12/2010 =A014:00:42
>
> >>>>> I am currently trying to achieve that if the time (last column) for
> >>>>> any of the above task is more than 2 hours (as compared to current
> >>>>> timing), then we need to get notified.
>
> >>>>> Can anyone advise how to go about it. I am currently using ksh
>
> >>>>> TIA
>
> >>>> This will print the number of hours difference and the task name for
> >>>> each line in your input file:
>
> >>>> gawk '{split($0,t,/[ /:]+/);
> >>>> print (systime() - mktime(t[4]" "t[2]" "t[3]" "t[5]" "t[6]" "t[7])) =
/
> >>>> 3600, $1}' file
>
> >>>> =A0 =A0 =A0 Ed.
>
> >>> Thanks Ed but it seems I do not have gawk in sh or ksh here. I get
> >>> following error
>
> >>> gawk: =A0not found
>
> >>> Any alternate way to go about it.
>
> >> Just install gawk (http://www.gnu.org/software/gawk/), you'll thank
> >> yourself later...
>
> >> =A0 =A0 =A0 =A0Ed.
>
> > If you are using a reasonably recent ksh93, then you can ignore Ed's
> > advice as ksh93 can do it with builtin commands.
>
> I get where you're coming from, but just because you can apparently do th=
is one
> thing with some constructs specific to some versions of ksh93 doesn't mea=
n it's
> a bad idea to install and use gawk for this and your future text processi=
ng
> needs instead.
>
> =A0 =A0 =A0 =A0 Ed.
>
> =A0 The key is to note that
>
>
>
>
>
> > printf "%(%#)T" now
>
> > will give you the time in seconds since the epoch, and you can substitu=
te
> > any reasonable phrase for "now", like "last monday".
>
> > So something like
>
> > while =A0 =A0 =A0read task day time
> > do integer age=3D$(printf "%(%#)T-%(%#)T" "$day $time" now)
> > =A0 =A0if =A0 =A0 =A0((age> =A02*3600))
> > =A0 =A0then =A0 =A0printf "%s is %d seconds old\n" "$task" $age
> > =A0 =A0fi
> > done
>
> > should do it.- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Icarus: There seems to be some problem as I keep getting syntax error.
Honestly, I truly did not understood how to above code works but just
wanted to try to see if this resolves the issue

This is what I tried

#!/usr/bin/ksh

while   read task day time
do      integer age=3D$(printf "%(%#)T-%(%#)T" "$day $time" "last
monday")
        if      ((age > 2*3600))
        then    printf "%s is %d seconds old\n" "$task" $age
        fi
done < File1.dat

test.ksh[3]: ()T-%#)T: syntax error

Ed: To be honest, I truly dont think I can run or install anything in
this environment until I have exhausted all other approaches.
0
Reply Pankaj 1/13/2010 5:29:47 PM

On Wed, 13 Jan 2010 09:29:47 -0800, Pankaj wrote:

>> > If you are using a reasonably recent ksh93, then you can ignore Ed's
>> > advice as ksh93 can do it with builtin commands.
>>
>> I get where you're coming from, but just because you can apparently do
>> this one thing with some constructs specific to some versions of ksh93
>> doesn't mean it's a bad idea to install and use gawk for this and your
>> future text processing needs instead.
>>
>>         Ed.
>>
>>   The key is to note that
>>
>>
>>
>>
>>
>> > printf "%(%#)T" now
>>
>> > will give you the time in seconds since the epoch, and you can
>> > substitute any reasonable phrase for "now", like "last monday".
>>
>> > So something like
>>
>> > while      read task day time
>> > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now)
>> >    if      ((age>  2*3600))
>> >    then    printf "%s is %d seconds old\n" "$task" $age fi
>> > done
>>
>> > should do it.- Hide quoted text -
>>
>> - Show quoted text -- Hide quoted text -
>>
>> - Show quoted text -
> 
> Icarus: There seems to be some problem as I keep getting syntax error.
> Honestly, I truly did not understood how to above code works but just
> wanted to try to see if this resolves the issue
> 
> This is what I tried
> 
> #!/usr/bin/ksh
> 
> while   read task day time
> do      integer age=$(printf "%(%#)T-%(%#)T" "$day $time" "last monday")
>         if      ((age > 2*3600))
>         then    printf "%s is %d seconds old\n" "$task" $age fi
> done < File1.dat
> 
> test.ksh[3]: ()T-%#)T: syntax error

What version of ksh are you running? You need a reasonably up-to-date 
ksh93. What does

printf "%T\n" now

say when you are running inside ksh? It should print out the date.
What does

echo "${.sh.version}"

say, it should give something like

Version JM 93t+ 2009-12-18

If there is nothing, or you get /usr/bin/ksh: ${.sh.version}: bad 
substitution then you are probably running ksh88 (and are probably on a 
sun). Often in that case you can get away with running /usr/dt/bin/dtksh, 
but unfortunately not in this case. dtksh is a ksh93 but not recent 
enough.

So in this case you probably have to fall back to this

http://groups.google.com/group/comp.unix.shell/msg/b17011dbda4bb76b

and in particular the timegm function.
0
Reply Icarus 1/14/2010 3:09:41 AM

On Jan 13, 10:09=A0pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
> On Wed, 13 Jan 2010 09:29:47 -0800, Pankaj wrote:
> >> > If you are using a reasonably recent ksh93, then you can ignore Ed's
> >> > advice as ksh93 can do it with builtin commands.
>
> >> I get where you're coming from, but just because you can apparently do
> >> this one thing with some constructs specific to some versions of ksh93
> >> doesn't mean it's a bad idea to install and use gawk for this and your
> >> future text processing needs instead.
>
> >> =A0 =A0 =A0 =A0 Ed.
>
> >> =A0 The key is to note that
>
> >> > printf "%(%#)T" now
>
> >> > will give you the time in seconds since the epoch, and you can
> >> > substitute any reasonable phrase for "now", like "last monday".
>
> >> > So something like
>
> >> > while =A0 =A0 =A0read task day time
> >> > do integer age=3D$(printf "%(%#)T-%(%#)T" "$day $time" now)
> >> > =A0 =A0if =A0 =A0 =A0((age> =A02*3600))
> >> > =A0 =A0then =A0 =A0printf "%s is %d seconds old\n" "$task" $age fi
> >> > done
>
> >> > should do it.- Hide quoted text -
>
> >> - Show quoted text -- Hide quoted text -
>
> >> - Show quoted text -
>
> > Icarus: There seems to be some problem as I keep getting syntax error.
> > Honestly, I truly did not understood how to above code works but just
> > wanted to try to see if this resolves the issue
>
> > This is what I tried
>
> > #!/usr/bin/ksh
>
> > while =A0 read task day time
> > do =A0 =A0 =A0integer age=3D$(printf "%(%#)T-%(%#)T" "$day $time" "last=
 monday")
> > =A0 =A0 =A0 =A0 if =A0 =A0 =A0((age > 2*3600))
> > =A0 =A0 =A0 =A0 then =A0 =A0printf "%s is %d seconds old\n" "$task" $ag=
e fi
> > done < File1.dat
>
> > test.ksh[3]: ()T-%#)T: syntax error
>
> What version of ksh are you running? You need a reasonably up-to-date
> ksh93. What does
>
> printf "%T\n" now
>
> say when you are running inside ksh? It should print out the date.
> What does
>
> echo "${.sh.version}"
>
> say, it should give something like
>
> Version JM 93t+ 2009-12-18
>
> If there is nothing, or you get /usr/bin/ksh: ${.sh.version}: bad
> substitution then you are probably running ksh88 (and are probably on a
> sun). Often in that case you can get away with running /usr/dt/bin/dtksh,
> but unfortunately not in this case. dtksh is a ksh93 but not recent
> enough.
>
> So in this case you probably have to fall back to this
>
> http://groups.google.com/group/comp.unix.shell/msg/b17011dbda4bb76b
>
> and in particular the timegm function.- Hide quoted text -
>
> - Show quoted text -

Thanks everyone. I was able to come up with following which is working
pretty good.

while read val1 val2 val3
do

start=3D$val3
end=3D`date +%H:%M:%S`

((start_mins =3D $(expr substr "$start" 1 2)*60 + $(expr substr "$start"
4 2)))
((end_mins =3D $(expr substr "$end" 1 2)*60 + $(expr substr "$end" 4
2)))
((elapsed_mins =3D end_mins - start_mins))

if [[ "$elapsed_mins" -lt 0 ]]
then
   ((elapsed_mins +=3D 1440))
fi

if [[ "$elapsed_mins" -gt 120 ]]
then
print $((elapsed_mins / 60)) hours and $((elapsed_mins % 60)) minutes
fi

done < file.txt
0
Reply Pankaj 1/14/2010 8:17:58 PM

On 2010-01-14, Pankaj <harpreet.noni@gmail.com> wrote:
> On Jan 13, 10:09 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
>> On Wed, 13 Jan 2010 09:29:47 -0800, Pankaj wrote:
>> >> > If you are using a reasonably recent ksh93, then you can ignore Ed's
>> >> > advice as ksh93 can do it with builtin commands.
>>
>> >> I get where you're coming from, but just because you can apparently do
>> >> this one thing with some constructs specific to some versions of ksh93
>> >> doesn't mean it's a bad idea to install and use gawk for this and your
>> >> future text processing needs instead.
>>
>> >>         Ed.
>>
>> >>   The key is to note that
>>
>> >> > printf "%(%#)T" now
>>
>> >> > will give you the time in seconds since the epoch, and you can
>> >> > substitute any reasonable phrase for "now", like "last monday".
>>
>> >> > So something like
>>
>> >> > while      read task day time
>> >> > do integer age=$(printf "%(%#)T-%(%#)T" "$day $time" now)
>> >> >    if      ((age>  2*3600))
>> >> >    then    printf "%s is %d seconds old\n" "$task" $age fi
>> >> > done
>>
>> >> > should do it.- Hide quoted text -
>>
>> >> - Show quoted text -- Hide quoted text -
>>
>> >> - Show quoted text -
>>
>> > Icarus: There seems to be some problem as I keep getting syntax error.
>> > Honestly, I truly did not understood how to above code works but just
>> > wanted to try to see if this resolves the issue
>>
>> > This is what I tried
>>
>> > #!/usr/bin/ksh
>>
>> > while   read task day time
>> > do      integer age=$(printf "%(%#)T-%(%#)T" "$day $time" "last monday")
>> >         if      ((age > 2*3600))
>> >         then    printf "%s is %d seconds old\n" "$task" $age fi
>> > done < File1.dat
>>
>> > test.ksh[3]: ()T-%#)T: syntax error
>>
>> What version of ksh are you running? You need a reasonably up-to-date
>> ksh93. What does
>>
>> printf "%T\n" now
>>
>> say when you are running inside ksh? It should print out the date.
>> What does
>>
>> echo "${.sh.version}"
>>
>> say, it should give something like
>>
>> Version JM 93t+ 2009-12-18
>>
>> If there is nothing, or you get /usr/bin/ksh: ${.sh.version}: bad
>> substitution then you are probably running ksh88 (and are probably on a
>> sun). Often in that case you can get away with running /usr/dt/bin/dtksh,
>> but unfortunately not in this case. dtksh is a ksh93 but not recent
>> enough.
>>
>> So in this case you probably have to fall back to this
>>
>> http://groups.google.com/group/comp.unix.shell/msg/b17011dbda4bb76b
>>
>> and in particular the timegm function.- Hide quoted text -
>>
>> - Show quoted text -
>
> Thanks everyone. I was able to come up with following which is working
> pretty good.
>
> while read val1 val2 val3
> do
>
> start=$val3
> end=`date +%H:%M:%S`
>
> ((start_mins = $(expr substr "$start" 1 2)*60 + $(expr substr "$start"
> 4 2)))
> ((end_mins = $(expr substr "$end" 1 2)*60 + $(expr substr "$end" 4
> 2)))
> ((elapsed_mins = end_mins - start_mins))
>
> if [[ "$elapsed_mins" -lt 0 ]]
> then
>    ((elapsed_mins += 1440))
> fi
>
> if [[ "$elapsed_mins" -gt 120 ]]
> then
> print $((elapsed_mins / 60)) hours and $((elapsed_mins % 60)) minutes
> fi
>
> done < file.txt

Won't your algorithm break as you cross midnight boundaries?

Regardless, if you have GNU date you could do something like:

NOWSEC="$(date +%s)"
while read taskid ts; do
  THENSEC="$(date -d "$ts" +%s)"
  if [ "$(( NOWSEC - THENSEC ))" -gt "$((2*60*60))" ]; then
    echo "${taskid} is over 2 hours old"
  fi
done < /tmp/test.txt

0
Reply Edgardo 1/15/2010 3:29:23 PM

9 Replies
365 Views

(page loaded in 0.164 seconds)

Similiar Articles:













7/25/2012 8:15:26 AM


Reply: