- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Get last months month in alpha?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 06:10 AM
тАО02-05-2007 06:10 AM
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.
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 07:01 AM
тАО02-05-2007 07:01 AM
Solution#!/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.
- Tags:
- caljd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 07:19 AM
тАО02-05-2007 07:19 AM
Re: Get last months month in alpha?
# let x=$(date +"%m")-1
# cal $(echo $x) $(date +%Y) | awk '{print substr($1,1,3);exit}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 07:45 AM
тАО02-05-2007 07:45 AM
Re: Get last months month in alpha?
[[ $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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 07:49 AM
тАО02-05-2007 07:49 AM
Re: Get last months month in alpha?
I had to do this one with pen and paper first...
$ echo "((($(date +%m)-1)+11)%12)+1"|bc
PCS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 07:52 AM
тАО02-05-2007 07:52 AM
Re: Get last months month in alpha?
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 08:04 AM
тАО02-05-2007 08:04 AM
Re: Get last months month in alpha?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 08:18 AM
тАО02-05-2007 08:18 AM
Re: Get last months month in alpha?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 08:43 AM
тАО02-05-2007 08:43 AM
Re: Get last months month in alpha?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-05-2007 09:57 AM
тАО02-05-2007 09:57 AM
Re: Get last months month in alpha?
$ 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