Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Single digit months to single digit?

SOLVED
Go to solution
Highlighted
Coolmar
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
James R. Ferguson
Acclaimed Contributor

Re: Single digit months to single digit?

Hi:

# date "+%1d"

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Single digit months to single digit?

Try the cmd below:

# date +'%0.m'
James R. Ferguson
Acclaimed Contributor
Solution

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...
Dennis Handly
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
Coolmar
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!
Oviwan
Honored Contributor

Re: Single digit months to single digit?

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

Regards
Pete Randall
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
spex
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
AwadheshPandey
Honored Contributor

Re: Single digit months to single digit?

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

Re: Single digit months to single digit?

Thanks!
Dennis Handly
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.