2

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
cxw
  • 16,685
  • 2
  • 45
  • 81
A.Midany
  • 201
  • 1
  • 5
  • 16
  • 2
    The error message `awk: syntax error near line 1; awk: bailing out near line 1` means you are calling old, broken awk (/usr/bin/awk on Solaris). Never use that awk. On Solaris use /usr/xpg4/bin/awk instead as in your second example or even better install GNU awk (which is what you are using on cygwin) which has its own builtin time functions and then you wouldn't need to mess around with `date`. – Ed Morton Jan 03 '17 at 15:23
  • If you are on Solaris 11 or later then you can use GNU date, which supports the `-d` option. That means using the `gdate` command instead of `date` command. If you are on earlier Solaris then you'll need to actively install GNU coreutils package (includes GNU date). – peterh Jan 04 '17 at 14:06

2 Answers2

1

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.

cxw
  • 16,685
  • 2
  • 45
  • 81
0

How to find yesterday?
Due to the daylight saving time, 24 hours ago can be today or the day before yesterday. When you want to change your script without using the GNU-date, use the following trick:

You are sure that yesterday is 20 or 30 hours ago. Which one? Well, the most recent one that is not today.

yesterday=$(printf "%s\n" "$(TZ=GMT+30 date +%Y-%m-%d)" "$(TZ=GMT+20 date +%Y-%m-%d)" |
    grep -v $(date +%Y-%m-%d) | tail -1)

When you want to see a difference of 24 hours (and want the time included), use something like

yesterdaynow="${yesterday} $(date +'%H:%M:%S')"

Be aware that this can be 23 or 25 hours ago, when switching DST.

Note:
When you have a "new" bash version, you can use other printf options, see https://stackoverflow.com/a/26804450/3220113
Using this you can can convert dates to epoch and calculate their difference.

Community
  • 1
  • 1
Walter A
  • 19,067
  • 2
  • 23
  • 43