AWK - Issues with script running at start of month.

I have a script that runs on an AIX 5.3.10.0. It runs perfectly on days that are double digit numbers, but from the 1st to the 9th of the month it runs but does not report anything.

The command in the script is as follows:

awk -v d=$(date '+%b%d') '/user:warn/ && /has shutdown/ && ($1$2 == d){ match($0,"The broker[^.][^.]*[.]");print $1,$2,$3 ":", substr($0, RSTART,RLENGTH)}' /var/mqsi/logs/CS/hub/user.log

I believe that there is an issue with the value that is being assigned to the $d variable, but can not figure out how to get around it. The way the variable is set at the moment with the %d displays the day of the month as a decimal number (01-31), using a 0 as leading space fill. In the file, there is no 0 padding, but an extra space is used (ie instead of 'Mar<space>06 08:27:49' the script will need to locate 'Mar<space><space>6 08:27:49').

I have tried substituting the %d with %e (Displays the day of the month as a decimal number (1-31). In a two-digit field, a blank space is used as leading space fill), and while this works when testing d=$(date '+%b%e') by itself, when I include it in AWK no results are produced.

As noted previously, I know this script works as if I run

awk '/user:warn/ && /has shutdown/ { match($0,"The broker[^.][^.]*[.]");print $1,$2,$3 ":", substr($0, RSTART,RLENGTH)}' /var/mqsi/logs/CS/hub/user.log

by itslef (not including the date parameters), a list of the required data is returned.

Can any one please advise what I need to do in order to make this script work all month round?

What you can do is to remove the leading zero before you feed into awk. Try this

set -- `date '+%b %d'`
mth=$1
day=${2#0}
awk -v d="$mth$day" '/user:warn/ && /has shutdown/ && ($1$2 == d){ match($0,"The broker[^.][^.]*[.]");print $1,$2,$3 ":", substr($0, RSTART,RLENGTH)}' /var/mqsi/logs/CS/hub/user.log
1 Like

Thankk you chihung, that was what I was after!