Single digit months to single digit? (210 Views)
Reply
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 1 of 12 (210 Views)
Accepted Solution

Single digit months to single digit?

How can I take the output from "date +%m" which is "09" and change it to just a "9"? I want all the months that start with a 0 (01, 01...09) to trim off the 0.

Thanks,
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 12 (210 Views)

Re: Single digit months to single digit?

Hi:

# date "+%1d"

Regards!

...JRF...
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 3 of 12 (210 Views)

Re: Single digit months to single digit?

Try the cmd below:

# date +'%0.m'
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 12 (210 Views)

Re: Single digit months to single digit?

Hi (again) :

Sorry, you said "month" not "day":

# date "+%2m" #...with leading space

# date "+%1m" #...without leading space

Regards!

...JRF...
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,895
Registered: ‎03-06-2006
Message 5 of 12 (210 Views)

Re: Single digit months to single digit?

There are some evil European locales where this may be done automatically.
for i in $(locale -a); do
echo $i ====
LANG=$i locale -k d_fmt| grep -v %d
done

For months use:
... grep -v -e %m -e %b
Please use plain text.
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 6 of 12 (210 Views)

Re: Single digit months to single digit?

Ok, the 'date "+%1m"' works perfectly, however come next month....which is 10 will the one be chopped off or will it remain 10? I just want any month with a leading 0, that the 0 is chopped.

Thanks for all the responses!
Please use plain text.
Honored Contributor
Oviwan
Posts: 692
Registered: ‎11-29-2005
Message 7 of 12 (210 Views)

Re: Single digit months to single digit?

Or use awk:
date "+%m" | awk '{ gsub(/^0?/,""); print $0 }'

Regards
Please use plain text.
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 8 of 12 (210 Views)

Re: Single digit months to single digit?

Works fine:

# date 10010805
Mon Oct 1 08:05:00 EDT 2007
# date "+%1m"
10


Pete

Pete
Please use plain text.
Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 9 of 12 (210 Views)

Re: Single digit months to single digit?

Alternatively, let the shell evaluate the output from 'date' as an integer:

$ echo $(( $(date "+%m") ))
9
Please use plain text.
Honored Contributor
AwadheshPandey
Posts: 737
Registered: ‎09-18-2005
Message 10 of 12 (210 Views)

Re: Single digit months to single digit?

date +%m 1|bc
It's kind of fun to do the impossible
Please use plain text.
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 11 of 12 (210 Views)

Re: Single digit months to single digit?

Thanks!
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,895
Registered: ‎03-06-2006
Message 12 of 12 (210 Views)

Re: Single digit months to single digit?

>the 'date "+%1m"' works perfectly, however come next month. which is 10 will the one be chopped off or will it remain 10?

The output of printf says it expands the field to contain the significant digits, it won't be truncated. Only %w.ps will truncate to the precision.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation