- Community Home
- >
- Servers and Operating Systems
- >
- Legacy
- >
- Operating System - Tru64 Unix
- >
- Re: How to extract last month from date field
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
тАО09-15-2008 05:19 PM
тАО09-15-2008 05:19 PM
How to extract last month from date field
I am trying to get 1 month prior date in shell scripting. For ex: If today's date is 09/15/2008, I would like to get 08/15/2008 and get only the month from that date. Ho wdo I achieve this is HP Tru64 UNIX. In Linux, I am using the following: STAMPMM=`date --date='1 month ago' +"%m"`
when I do an echo of $STAMPMM, it displays 08 as the month. This is not working in HP Tru64 UNIX. How do I achieve the same on HP Tru64 UNIX?
Thanks
Aishwarya.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-15-2008 07:27 PM
тАО09-15-2008 07:27 PM
Re: How to extract last month from date field
program, which, I gather, is part of the
"coreutils" package:
http://www.gnu.org/software/coreutils/
Otherwise, sed or awk or something could be
used to extract the various fields from a
date string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2008 04:04 AM
тАО09-16-2008 04:04 AM
Re: How to extract last month from date field
What else are you going to do with the prior month, once you have it. The next step(s) might be easier from perl then from the shell
perl -e "$m = (localtime)[4]; printf qq(%02d), ($m)? $m: 12"
The 4'th element in he array return by localtime is the 0-based month number.
So the substraction happens to be done alerady, but you do have to deal with january -> december for example as per above
What other date/time transformation do you need? Some (hpuc) folks like Clay Stephenson's "Date Hammer". SOurce linked from: http://www.cmve.net/~merijn/index.html#Contrib
hth,
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2008 05:01 AM
тАО09-16-2008 05:01 AM
Re: How to extract last month from date field
integer LM
integer M=`date +%m`
if (( M == 1 ))
then
LM=12
else
let LM=M-1
fi
Not particularly elegant, but it works. :)
Martin
A quick resolution to technical issues for your HPE products is just a click away HPE Support Center
See Self Help Post for more details
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2008 06:39 AM
тАО09-16-2008 06:39 AM
Re: How to extract last month from date field
Thanks
Aishwarya.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2008 06:47 AM
тАО09-16-2008 06:47 AM
Re: How to extract last month from date field
More importantly (IMHO) it opens up the world of localtime/timelocal in perl.
For example, a clean way to get yesterdays day is:
$ perl -e "$d = (localtime)[3]; printf qq(%02d), $d"
A better solution for the original problem in perl would be:
$ perl -e "$d=(localtime)[3]; $m=1+(localtime(time - $d*86400))[4]; printf qq(%02d), $m"
That is: subtract as many seconds in a day times as many days in this months from the current time, givign the last day of last month. Now get the month part for that.
Full definition of localtime can of course be foudn in the (online) perl docs but here is the highlight:
# 0 1 2 3 4 5 6 7 8
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2008 07:55 AM
тАО09-16-2008 07:55 AM
Re: How to extract last month from date field
There are different things you can do to delete the old file depending on things like the format of the file name, when you run the cleanup script, etc. For example, if the previous month's report has a name like report-monthly.M (where M is the month), then using the LM variable from my ksh example, you could just do:
rm report-monthly.${LM}
Or if it's more complex, the find command could be your friend. Suppose the reports have different names, with some parts of the name in common, but you know they're all created no later than the last day of the previous month. If you run the cleanup script on the 15th of the month, you could do something like:
find
-name 'report*' matches all files whose names begin with "report".
-ctime +15 matches all files created at least 15 days ago.
-exec rm {} \; runs the rm command on each file that matches all the above criteria.
Change this up as needed for the specifics of your particular situation. Cautionary note: before implementing a find command that deletes or alters your files, be sure to check it out thoroughly on a small scale first!
Martin
A quick resolution to technical issues for your HPE products is just a click away HPE Support Center
See Self Help Post for more details
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-16-2008 08:12 AM
тАО09-16-2008 08:12 AM
Re: How to extract last month from date field
Perl can take care of all of that.
Let's assume your file needs to be called : tmp.MM
With the hack construct:
perl -e '$m =(localtime)[4]; $f=sprintf q(tmp.%02d), ($m)? $m: 12; unlink $f'
With the more generic date math construct:
perl -e '$d=(localtime)[3]; $f=sprintf q(tmp.%02d),1+(localtime(time - $d*86400))[4]; unlink $f'
btw 1: For testing replace the 'unlink' with 'print'
btw 2: For those curious... I use qq and q instead of doublequote and single quotes because I oftent test on OpenVMS or Windoze where the command line quote handling gets obnoxious
Hein.