- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Get next/last month from current date
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
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
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
05-31-2016 06:48 PM - edited 05-31-2016 06:49 PM
05-31-2016 06:48 PM - edited 05-31-2016 06:49 PM
Get next/last month from current date
Hi all
i want to get next/last month from current date from HP-UX system (HP-UX B.11.23 )
example :
current day : 160601
i want to get output :
160701 (next month)
160501 (last month)
Any suggestion ?
Thx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2016 10:54 PM
05-31-2016 10:54 PM
Re: Get next/last month from current date
> i want to get next/last month from current date from HP-UX system
Are you looking for a shell script, or what? Assuming a shell
script, how much don't you know how to do? Some potentially useful
pieces follow.
date='160601'
mba$ echo $date | sed -e 's/\(..\)..../\1/'
16
mba$ echo $date | sed -e 's/..\(..\)../\1/'
06
mba$ echo $date | sed -e 's/....\(..\)/\1/'
01
mba$ m='06'
mba$ echo '-01-02-03-04-05-06-07-08-09-10-11-12-01' | \
sed -e "s/.*-$m-\(..\).*/\1/"
07
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2016 02:05 PM
06-01-2016 02:05 PM
Re: Get next/last month from current date
There are a lot of requirements that you did not provide.
There is nothing built-in for HP-UX for this requirement.
Here is a scripted solution (two functions) where you can provide any year (00-99) and month (01-12). You simply add these functions to your main program.
function PrevMON { # Provide YYMM01 # Returns YYMM01 for previous month # Handles year change for MON=1 # Wrong parm count if [[ $# -ne 1 ]] then echo "999999" return fi CURRMO=$(echo $1 | cut -c 3-4) CURRYR=$(echo $1 | cut -c 1-2) # parms not in range if [[ $CURRYR < "00" || $CURRYR > "99" ]] then echo "YR??" return fi if [[ $CURRMO < "01" || $CURRMO > "12" ]] then echo "MO??" return fi typeset -Z2 YRMINUS=$CURRYR typeset -Z2 MONMINUS=$(($CURRMO-1)) if [[ $MONMINUS -lt 1 ]] then MONMINUS=12 YRMINUS=$(($CURRYR-1)) fi printf "%2s%2s01" $YRMINUS $MONMINUS }
and
function NextMON { # Provide YYMM01 # Returns YYMM01 for previous month # Handles year change for MON=1 # Wrong parm count if [[ $# -ne 1 ]] then echo "999999" return fi CURRMO=$(echo $1 | cut -c 3-4) CURRYR=$(echo $1 | cut -c 1-2) # parms not in range if [[ $CURRYR < "00" || $CURRYR > "99" ]] then echo "YR??" return fi if [[ $CURRMO < "01" || $CURRMO > "12" ]] then echo "MO??" return fi typeset -Z2 YRPLUS=$CURRYR typeset -Z2 MONPLUS=$(($CURRMO+1)) if [[ $MONPLUS -gt 12 ]] then MONPLUS=1 YRPLUS=$(($CURRYR+1)) fi printf "%2s%2s01" $YRPLUS $MONPLUS }
Examples:
echo $(PrevMON 020201)
020101
echo $(PrevMON 020101)
021201
echo $(NextMON 021101)
021201
echo $(NextMON 021201)
030101
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2016 05:10 PM
06-01-2016 05:10 PM
Re: Get next/last month from current date
> There are a lot of requirements that you did not provide.
As usual.
Also as usual, there are many ways to solve a problem like this.
Many (most?) will involve disassembling the YYMMDD-format date string
into separate YY, MM, and DD pieces; doing the arithmetic/string
processing; and then reassembling the pieces to make a new YYMMDD-format
date string. There is more than one way to do each of these tasks. (As
shown here.)
> function PrevMON
> [...]
> # Returns YYMM01 for previous month
> # Handles year change for MON=1
> function NextMON
> [...]
> # Returns YYMM01 for previous month
> # Handles year change for MON=1
Too much copy+paste in the comments.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2016 06:50 PM
06-01-2016 06:50 PM
Re: Get next/last month from current date
Excellent catch.
Here are fixed lines for the NextMON function:
> # Returns YYMM01 for next month > # Handles year change for MON=12
Bill Hassell, sysadmin