0

I have an input file like below;

975700 18:32:32.956
975700 18:32:32.971
975700 18:32:33.018
975701 18:32:32.987
975702 18:32:32.987
975702 18:32:33.003
975702 18:32:33.034

I would like to take the time difference of the same ID numbers, so far I am able to come up with the code below;

while read jobnum time
do
    if [[ "$prevjobnum" != "$jobnum" ]] && [[ `grep $jobnum $joblst|wc -l` -eq 1 ]]
    then
        echo "$jobnum incomplete log"
    elif [[ "$prevjobnum" == "$jobnum" ]]
    then
        ENDTIME=`date +%s.%N -d "$time"`
        STARTTIME=`date +%s.%N -d "$prevtime"`
        DIFF=$(echo $ENDTIME - $STARTTIME | bc | sed 's/0\{1,\}$//')

        echo "$jobnum clip time is $DIFF seconds"
    else
        :
    fi

However it only works on one and two instance of the ID. I would also like to make it work so that it can do the difference for the first and last instance of ID in case it is more than two like below;

975700 18:32:32.956
975700 18:32:32.971
975700 18:32:33.018

Would like to get an output like below;

975700 clip time is .062 seconds
975701 incomplete log
975702 clip time is .047 seconds
Dren
  • 319
  • 2
  • 14
  • I see this is the continuation of [your previous question](http://stackoverflow.com/q/39240688/1983854). You are still missing the desired output, which you said it was something and then accepted an answer that did not provide it. Please, do [edit] this post and clarify accordingly. – fedorqui Aug 31 '16 at 14:05
  • @Inian Edited the question to include desired o/p – Dren Aug 31 '16 at 14:06
  • @fedorqui I opt to post another question because previously I got feedback that if an initial question is already answered and another question is asked it should be done on a new thread. – Dren Aug 31 '16 at 14:11
  • Yes, no problem about this. I am just pointing out what was missing in the previous one, so that this one is more clear. – fedorqui Aug 31 '16 at 14:12
  • 2
    Also, what is this "incomplete log" line? Just stick to a [mcve]. – fedorqui Aug 31 '16 at 14:14
  • @fedorqui the "incomplete" is just a sample comment i came up with if the the ID is of single instance only – Dren Aug 31 '16 at 14:16
  • @Inian I have modified the sample input to indicate a scenario where there is a single instance – Dren Aug 31 '16 at 14:20
  • Uf now I see the link in my first comment was not accurate. You posted yet another question since the one I participated into: [if condition on while iteration loop bash](http://stackoverflow.com/q/39223792/1983854). – fedorqui Aug 31 '16 at 14:21

1 Answers1

1

The following script might be a good starting point:

#!/bin/bash

while read jobnum time
do
  # first line
  if [[ -z $prevjobnum ]]; then
    prevjobnum=$jobnum
    starttime=$time
    lasttime=$time
    continue
  fi

  if [[ $prevjobnum == $jobnum ]]; then
    lasttime=$time
  else

    if [[ $starttime == $lasttime ]]; then
      echo "$prevjobnum incomplete log"
    else
      echo "$prevjobnum $starttime $lasttime"
      :
    fi

    prevjobnum=$jobnum
    starttime=$time
    lasttime=$time
  fi

done

if [[ $starttime == $lasttime ]]; then
  echo "$prevjobnum incomplete log"
else
  echo "$prevjobnum $starttime $lasttime"
fi

Output

975700 18:32:32.956 18:32:33.018
975701 incomplete log
975702 18:32:32.987 18:32:33.034
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
  • After incorporating the time computation to your code recommendation it worked just the way i would want it. Many thanks. – Dren Aug 31 '16 at 22:55