Find time difference between two consecutive lines in same file.

Hello
I have a file in following format:

IV 08:09:07
NM 08:12:01
IC 08:12:00
MN 08:14:20
NM 08:14:15

I need a script to compare time on each line with previous line and show the inconsecutive line. Ex.:
08:12:00
08:14:15

A better way will be a message in this form:
�IC has 1 second difference on 08:12:00 time
NM has 5 second difference on 08:14:15 time�
But first example will be more than enough.

How i can compare each line whit previews line? I tried this script:

 
  cat myfile|gawk '{print $2}'>tempfile
   while read line
    do
    PAST=$line -1 ****** someheting similar here
    DIFF=$(($PAST-$LINE))
   echo $line $DIFF
  done<tempfile
   

but it doesn�t work in line 3. Is a syntax problem or is an impossible command. I don�t know. I�m new in this. I even try to make a variable name, variable but again I�m stuck in line 3 on syntax or in �impossibliness� problem:

    while read line ;do
     for ((  i = 1 ;  i <= 5;  i++  ))
      PAST${!i}=$line   **********!!!????
      DIFF=$(($PAST${!i}-$(($PAST${!i-1}))
   echo $DIFF
   done
  done < tempfile

It must be an �eval� expression somewhere but �indirect variable reference� is way over my head right now. I have a felling that all can be done very easily but I can�t see how :wall:.
Thanks in advance for your time.

Your requirment and/or data sample are not very clear, however base you your post I can provide this solution

# cat file
IV 08:09:07
NM 08:12:01
IC 08:12:00
MN 08:14:20
NM 08:14:15

# awk 'function s(x){split($NF,t,":");return t[1]*60^2+t[2]*60+t[3]}{cur=s($NF);if(prev){diff=cur-prev};prev=cur;if(diff<0){print $1" has "diff*-1" second difference on "$NF}}' file
IC has 1 second difference on 08:12:00
NM has 5 second difference on 08:14:15

if you need a plain ksh example of a utility script, you could do the following:-

#!/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:-

#!/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:

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

Thanks for reply. It is a very elaborate response. I'm eager to try your solutions. Unfortunately all my testing data are at work. I will test your script Monday and I will keep you posted. Thanks again.

I promised to get back with results.
The danmero solution is working charmingly, although I'm not fully understand how it read previous line and compare with current one. The data from my file are several lines long and script pointed me exactly where to look for differences. Thanks again danmero, this is exactly what I need.
Although is not exactly what I intended to obtain, I tested the rbatte1 solution and after choosing h or S option I get an error:

Please enter the start time: 08:09:10
Please enter the end time: 08:09:20
Respond in hh:mm:ss or seconds? (h/S)S
The difference is: ./rba[26]: shhs=*3600: unexpected `*'
./rba[27]: ehhs=*3600: unexpected `*'
./rba[28]: smms=*60: unexpected `*'
./rba[29]: emms=*60: unexpected `*'
0

Probably becouse i use bash which i must be mentioned earlyer. Sorry.
Thanks again for your time. My problem was solved :b:

Ahh. I wrote for ksh. What input did you supply? I will have a go at converting.

The input was hh:mm:ss format.
In this case the start time was 08:09:10 and the end time was 08:09:20