Skip to content
Advertisement

Comparing epoch timestamps in a log and checking if x time has elapsed between the previous and current timestamp in the list

I have a log file consisting of a list of epoch 19-digit timestamps, for example

1603871402884765526
1603871402884859949
1603871402885343431
1603871402885403005
1603871402932368738
1603871402932735285
1603871402932741891
1603871402932785964
1603871402934480339
1603871402934539183

I would like to iterate through this list, and compare the previous timestamp to the current timestamp in the list and echo a message if x duration of inactivity as occurred, ex:

diff=$(( $previoustimestamp - $currenttimestamp ))
if [ $diff -gt 5000000000 ] 
then
  echo "Inactivity between $previoustimestamp and $currenttimemstap: $diff nanoseconds" 
  continue
else
  continue 
fi

What would be the most efficient method to iterate through the list such that the current timestamp becomes the previous time stamp, and so forth, until I reach the end of the list?

Many thanks

Advertisement

Answer

I suggest this simple awk for this:

awk -v delta='400000' 'NR > 1 {
   diff = $1 - prev
   if (diff > delta)
      print "Inactivity between", prev, "and", $1 ":", diff, "nanoseconds"
}
{
   prev = $1
}' file.txt
Inactivity between 1603871402884859949 and 1603871402885343431: 483584 nanoseconds
Inactivity between 1603871402885403005 and 1603871402932368738: 46965760 nanoseconds
Inactivity between 1603871402932785964 and 1603871402934480339: 1694464 nanoseconds

You can tune your desired value of delta in argument.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement