Operating System - Tru64 Unix
1752609 Members
4350 Online
108788 Solutions
New Discussion юеВ

Re: How to extract last month from date field

 
AKB_1
Occasional Contributor

How to extract last month from date field

Hi,

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.
7 REPLIES 7
Steven Schweda
Honored Contributor

Re: How to extract last month from date field

One way might be to get the GNU "date"
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.
Hein van den Heuvel
Honored Contributor

Re: How to extract last month from date field

Just use perl, on any platform?

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


Martin Moore
HPE Pro

Re: How to extract last month from date field

The following works in ksh on Tru64 UNIX. M is the current month (1-12) and LM is the previous month.

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
I work for HPE
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

Accept or Kudo

AKB_1
Occasional Contributor

Re: How to extract last month from date field

Thanks Steven, Hein and Martin! I tried Martin's solution and it worked. I want to delete a particular file with prior month's timestamp. How can that be achieved?

Thanks
Aishwarya.
Hein van den Heuvel
Honored Contributor

Re: How to extract last month from date field

My perl, as per Martin, is not very elegant, but it may do.

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);

Martin Moore
HPE Pro

Re: How to extract last month from date field

I tried to post this earlier, but it seems that the bit bucket got it.

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 -xdev -name 'report*' -ctime +15 -exec rm {} \;

is the top-level directory you want to run this in. -xdev is to keep it from crossing mount points (a precaution).

-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
I work for HPE
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

Accept or Kudo

Hein van den Heuvel
Honored Contributor

Re: How to extract last month from date field

Ah, you want to use the month string to construct a file name to delete.
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.