Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Single digit months to single digit?

SOLVED
Go to Solution
Esteemed Contributor Esteemed Contributor

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,
11 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: Single digit months to single digit?

Hi:

# date "+%1d"

Regards!

...JRF...
Honored Contributor Honored Contributor

Re: Single digit months to single digit?

Try the cmd below:

# date +'%0.m'
Acclaimed Contributor Acclaimed Contributor

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...
Acclaimed Contributor Acclaimed Contributor

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
Esteemed Contributor Esteemed Contributor

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!
Honored Contributor Honored Contributor

Re: Single digit months to single digit?

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

Regards
Outstanding Contributor Outstanding Contributor

Re: Single digit months to single digit?

Works fine:

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


Pete

Pete
Honored Contributor

Re: Single digit months to single digit?

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

$ echo $(( $(date "+%m") ))
9
Honored Contributor Honored Contributor

Re: Single digit months to single digit?

date +%m 1|bc
It's kind of fun to do the impossible
Esteemed Contributor Esteemed Contributor

Re: Single digit months to single digit?

Thanks!
Highlighted
Acclaimed Contributor Acclaimed Contributor

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.