Reputation: 319
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
Upvotes: 0
Views: 122
Reputation: 20843
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
Upvotes: 1