Search code examples
bashshelldateawksolaris

bash: How to compare two dates on Solaris without date -d option


I'm trying to compare the date input from lines of a log file with date of yesterday and if the difference is more than one day, then it prints that line from the log file.

Log file:

$more ActiveX2Alarms.log
2016-09-30 01:40:14 MET;faultManager:network@ET_AO_L_0165_abcde@eNBEquip@eNBInst@x2Grp-0@x2Access-0_424_02_ET_AO_L_0165_abcde_44159@x2Transp-0|alarm-2632-3-698;ET_AO_L_0165_abcde;major;2632
;lte.IK4009022;3;698
2016-11-01 08:10:51 MET;faultManager:network@ER_AO_L_4283_abcde@eNBEquip@eNBInst@x2Grp-0@x2Access-0_424_02_ER_AO_L_4283_abcde_14179@x2Transp-0|alarm-2632-3-698;ER_AO_L_4283_abcde;minor;2632;lte.IK4009022;3;698
2017-01-03 12:14:31 MET;faultManager:network@EM_AO_L_4065_abcde@eNBEquip@eNBInst@x2Grp-0@x2Access-0_424_02_EM_AO_L_4065_abcde_44094@x2Transp-0|alarm-2632-3-698;EM_AO_L_4065_
abcde;minor;2632;lte.IK4009022;3;698

I have created test environment on Windows using cygwin. In Cygwin, i have successful o/p.

Here is the initial code i used:

awk -v d="$(date -d "yesterday" +'%Y-%m-%d %H:%M:%S')" '$1 " " $2 < d' /cygdrive/f/Script_X2/Final/Last_Trial/ActiveX2Alarms.log

Result:

2016-09-30 01:40:14 MET;faultManager:network@ET_AO_L_0165_abcde@eNBEquip@eNBInst@x2Grp-0@x2Access-0_424_02_ET_AO_L_0165_abcde_44159@x2Transp-0|alarm-2632-3-698;ET_AO_L_0165_abcde;major;2632
;lte.IK4009022;3;698
2016-11-01 08:10:51 MET;faultManager:network@ER_AO_L_4283_abcde@eNBEquip@eNBInst@x2Grp-0@x2Access-0_424_02_ER_AO_L_4283_abcde_14179@x2Transp-0|alarm-2632-3-698;ER_AO_L_4283_abcde;minor;2632;lte.IK4009022;3;698

However when using same command on the main server i received an error:

date: illegal option --d

Searching for the error i understood it is related to the installed Solaris packages

$uname -a
SunOS xxxxxx 5.10 Generic_150400-15 sun4u sparc SUNW,SPARC-Enterprise

$date --version
date: illegal option -- version
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
$date --help
date: illegal option -- help
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]

So i used following code to avoid this issue, however it is not giving any output...so something is still wrong, please help!

$awk -v YESTERDAY="`TZ=GMT+20 date +'%d-%m-%Y %H:%M:%S'`" '$1 " " $2 < YESTERDAY' ActiveX2Alarms.log
awk: syntax error near line 1
awk: bailing out near line 1

$/usr/xpg4/bin/awk -v YESTERDAY="`TZ=GMT+20 date +'%d-%m-%Y %H:%M:%S'`" '$1 " " $2 < YESTERDAY' ActiveX2Alarms.log
$

To confirm YESTERDAY variable assignment is successful:

$YESTERDAY="`TZ=GMT+20 date +'%d-%m-%Y %H:%M:%S'`"; echo $YESTERDAY
02-01-2017 16:51:26

Solution

  • Your date formats are different between the log file and YESTERDAY, but should match. Since the log file is in Y-M-D order, use that:

    awk -v YESTERDAY="`TZ=GMT+20 date +'%Y-%m-%d %H:%M:%S'`" '($1 " " $2) < YESTERDAY' ActiveX2Alarms.log
                                        ^^^^^^^^ was %d-%m-%Y
    

    On my test system (also cygwin), this gives the same two lines of output you showed in your question.

    This is because awk is comparing strings, not dates, so the dates have to be in Y-M-D to sort numerically. awk will not automatically convert the string of the date to an actual date (as far as I know). I added parentheses to ($1 " " $2) to clarify that you are making a string that will be compared to another string.

    Caution: While I think your TZ=GMT+20 hack is very cool, GMT+20 is only 19 hours ahead of MET, not 24 hours. Therefore, YESTERDAY will not be set correctly if you run the script at the wrong time of day. This answer has an example of how to get yesterday's date in bash without regard to timezone.