Compare timestamps in a text file

I needed to compare 2 timestamps in a log file and come up with the newest.

Here’s the little script I wrote:

  1. I needed to remove the blanks and non-numerical characters
  2. Then compare the numerical strings

Before proceeding make sure to convert your string in this order: year, month, day, hh24, mi, ss

#!/bin/sh

DATE1=”2008 September 12 11:30:45″

DATE2=”2008 September 12 12:45:32″

dateval1=`print ${DATE1} | sed ‘s/[^0-9]//g’`

echo $dateval1

20080912113045

echo $dateval2

20080912124532

if [ $dateval1 -gt $dateval2 ]; then
echo $dateval1
else
echo $dateval2
fi

Advertisements