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


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

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

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

echo $dateval1


echo $dateval2


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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s