Operating System - Linux
1753859 Members
7459 Online
108809 Solutions
New Discussion юеВ

Single digit months to single digit?

 
SOLVED
Go to solution
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 11
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