Re: Get last months month in alpha? (386 Views)
Reply
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 1 of 14 (386 Views)
Accepted Solution

Get last months month in alpha?

Hi,

Is there a simple way to get last month's month in alpha chars. For example, this month is "Feb", I need it to output last month "Jan"

Thanks,
S.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 2 of 14 (386 Views)

Re: Get last months month in alpha?

Here's one approach using caljd.sh:

#!/usr/bin/sh

caljd.sh $(caljd.sh) | read MO DUMMY YEAR
MON=$(caljd.sh -M -o $(caljd.sh -p 1 ${MO} 1 ${YEAR}))
echo "Month = ${MON}"

It works by determining the month day and year or the current month, then setting the day of the month to 1 and finding the Julian day of the previous day and then outputting the monthname. Change the "-o" in the last line to "-O" and the full month name will appear -- and full National Language Support works if LANG is set.

Here's caljd.sh; invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 3 of 14 (386 Views)

Re: Get last months month in alpha?

Here's something that you could try though I would prefer to use caljd.sh

# let x=$(date +"%m")-1
# cal $(echo $x) $(date +%Y) | awk '{print substr($1,1,3);exit}'
Honored Contributor
Denver Osborn
Posts: 1,749
Registered: ‎06-23-2000
Message 4 of 14 (386 Views)

Re: Get last months month in alpha?

THIS_MONTH=`date +%b`
[[ $THIS_MONTH = Jan ]] && LAST_MONTH=Dec
[[ $THIS_MONTH = Feb ]] && LAST_MONTH=Jan
[[ $THIS_MONTH = Mar ]] && LAST_MONTH=Feb
[[ $THIS_MONTH = Apr ]] && LAST_MONTH=Mar
..and so on..
echo Last month was $LAST_MONTH


-denver
Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 5 of 14 (386 Views)

Re: Get last months month in alpha?

Hi Coolmar,

I had to do this one with pen and paper first...

$ echo "((($(date +%m)-1)+11)%12)+1"|bc

PCS
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 14 (386 Views)

Re: Get last months month in alpha?

Hi:

Another:

# perl -le '$m=shift or die;@mon=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan);
$n = $m == 1 ? 11:$m-2;print @mon[$n]' month_number

Regards!

...JRF...
Honored Contributor
spex
Posts: 1,367
Registered: ‎05-14-1996
Message 7 of 14 (386 Views)

Re: Get last months month in alpha?

Oops... I missed the alpha part with my response above...

set -A mo Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec && echo ${mo[$(echo "((($(date +%m)-1)+11)%12)"|bc)]}

PCS
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 8 of 14 (386 Views)

Re: Get last months month in alpha?

Hi,

a simple ksh array solution:

set -A mo NULL Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
print ${mon[$(date +%m)]}

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 9 of 14 (386 Views)

Re: Get last months month in alpha?

Hi (again):

I agree with Peter N. :

If you unconditionally want the previous month without providing a month number as an arguement, then my Perl solution can be reduced to:

# perl -le '@mon=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);$n=(localtime)[4];$n = $n == 0 ? 11:$n-1;print @mon[$n]'

Regards!

...JRF...
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 10 of 14 (386 Views)

Re: Get last months month in alpha?

Along the same lines:

$ perl -e "print substr(qq(DecJanFebMarAprMayJunJulAugSepOctNov),3*(localtime)[4],3)"
Jan
$

Without listing the month names in perl, but much more work :-)

Not useful here, but the principle may help

perl -e "use Time::Local; @t=localtime; if($t[4]) { $t[4]-- } else {$t[5]--;$t[4]=12} $t=timelocal(@t); $m=(split(" ",scalar l
ocaltime($t)))[1]; print $m"

Jan


use Time::Local; Find source
@t=localtime; Now as array ($sec,$min,$hour,$mday,$mon,$year);
if($t[4]); Anything but Jan?
{ $t[4]-- } Just decrement the month
else {
$t[5]--;$t[4]=12} Month = 12
$t=timelocal(@t); Time in seconds
$m=(split(" ", 2nd word
scalar localtime($t)))[1]; Time as text
print $m" Finally!

Hein
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 11 of 14 (386 Views)

Re: Get last months month in alpha?

I would prefer a more portable version that leverages "locale LC_TIME" for NLS but that's just me. With caljd.sh you get that for free.
If it ain't broke, I can fix that.
Acclaimed Contributor
Dennis Handly
Posts: 25,198
Registered: ‎03-06-2006
Message 12 of 14 (386 Views)

Re: Get last months month in alpha?

>Clay: I would prefer a more portable version that leverages "locale LC_TIME"

You could take the output from "locale abday", strip the ";" and double quotes and fill your array.
$ set -A WEEKDAY $(locale abday | sed -e 's/[;"]/ /g')
$ echo ${WEEKDAY[@]}
Sun Mon Tue Wed Thu Fri Sat
Acclaimed Contributor
Dennis Handly
Posts: 25,198
Registered: ‎03-06-2006
Message 13 of 14 (386 Views)

Re: Get last months month in alpha?

Oops, Coolmar wanted months:
$ set -A MONTHS $(locale abmon | sed -e 's/[;"]/ /g')
$ echo ${MONTHS[*]}
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

And to print out last month:
echo ${MONTHS[($(date +%m)+10)%12]}
Esteemed Contributor
Coolmar
Posts: 1,074
Registered: ‎12-21-2004
Message 14 of 14 (386 Views)

Re: Get last months month in alpha?

Thanks everyone for all the suggestions!
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.