- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Date format
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
тАО07-26-2009 06:18 PM
тАО07-26-2009 06:18 PM
MTHLESS="$((`date +%m`-1))"
using the above command,I am able to get the current month -1, I would like to know whether it will work when it comes to January. Will I be able to get December as the output?
Please advice.
Regards
Feng Lin
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 01:02 AM
тАО07-27-2009 01:02 AM
Re: Date format
01 - 1 = 0
Which I suspect is not what you want...
Why not install the GNU version of date which is available in the GNU coreutils package available here:
http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-7.4/
(Don't miss the runtime dependencies you need too)
Once you have that you can easily do things like this:
# /usr/local/coreutils/bin/date +%m
07
# /usr/local/coreutils/bin/date -d "last month" +%m
06
HTH
Duncan
I am an HPE Employee
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 02:16 AM
тАО07-27-2009 02:16 AM
Re: Date format
You can get 12 if you do:
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 03:51 AM
тАО07-27-2009 03:51 AM
Re: Date format
If the month is all that you want, you can do:
# awk -v $(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 08:27 AM
тАО07-27-2009 08:27 AM
Re: Date format
I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?
Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 08:38 AM
тАО07-27-2009 08:38 AM
Re: Date format
> Dennis: I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?
I never said that is was. I'm merely pointing out TIMTOWDI ;-)
> Dennis: Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.
The BEGIN rule takes care of that _if_ I hadn't bungled the variable assignment:
# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 05:24 PM
тАО07-27-2009 05:24 PM
Re: Date format
Thanks for the info.
Now I have the following situation : -
I have files with naming convention *yyyymmdd*. How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?
I intend to run the housekeeping job twice a month on the 1st and 16th of the month. 1st is to archive 16 - 31st day files, 16th is to archive 1 - 15th day files.
Thanks a lot.
Regards
Feng Lin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 05:39 PM
тАО07-27-2009 05:39 PM
Re: Date format
> How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?
# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}'
06
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-27-2009 06:03 PM
тАО07-27-2009 06:03 PM
SolutionI guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN.
I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.
>How am I going to take care of the zero (fill) (Jan - Sep) and one(Oct - Dec) for the month
typeset -Z2 MTHLESS # zero fill
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
echo $MTHLESS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2009 04:18 AM
тАО07-28-2009 04:18 AM
Re: Date format
> Dennis: I guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN. I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.
I agree that the input file(s) are not read in the case of a BEGIN block with no pattern-action statements in the subsequent main block. This is easy to demonstrate.
Of course, this behavior doesn't occur if an END block is declared either alone or in concert with a BEGIN block. In that case, at least one file must be input.
I concede that relying on these subtitles may lead to confusion and/or false expectations. Hence it is probably best to amend the 'awk' snippet to:
# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null
...and then (similarly) this works too:
# awk -v MON=$(date +%m) 'END{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null
Regards!
...JRF...