need help scripting a date when the date is 1st to 9th

I'm new to scripting, I am trying to get the current month and date in my script I have this line

MYDATE=`date '+%b %d'`

this works for dates 10-31, but it returns a 09 for the 9th, where the file name has a 9,

I tried

MYDATE=`date '+%b %_d'`

but that returns Jul %_d

I need it to return Jul 9 and not Jul 09

I just need to get the current month date and pull any lines that match it from a log file

DATE(P) 		   POSIX Programmer's Manual		       DATE(P)

       %e     Day of the month as a decimal number [1,31] in a two-digit field
	      with leading space character fill.

Will this do? Or you need to trim the space too?

1 Like

Try this, it should lose the extra space from the %e.

MYDATE=`date '+%b %e' | tr -s " "`

However if it turns out that you need that extra space (As you often do when looking at system logs):

MYDATE="`date '+%b %e'`"

Pity you didn't post this yesterday!

1 Like

Here is a way of testing: (linux)

date -d " 1 day ago" '+%b %e'
Jul  9
1 Like

Thank you this worked like a charm

 
MYDATE="`date '+%b %e'`"

And for unix in timezones near the meridian (not an accurate way of getting yesterday's date):

TZ=GMT+24 date '+%b %e'|tr -s " "
Jul 9

TZ=GMT+24 date '+%b %e'
Jul  9