1745896 Members
4205 Online
108723 Solutions
New Discussion юеВ

Re: Date manipulation

 
SOLVED
Go to solution
Jeeshan
Honored Contributor

Date manipulation

I have some scripts which are using for different purposes, like disk monitoring, log monitoring etc. etc.But I have a problem regarding date manipulation.

Say on 1st April, I have to check the logs of yesterday which is 31st March. Then how would my system will act according to the month whatever comes.

what would be the solution?
a warrior never quits
8 REPLIES 8
Solution

Re: Date manipulation

Clay's date hammer, available here:

http://www.cmve.net/merijn/caljd-2.25.sh

resolves most of these date manipulation challenges...

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Jeeshan
Honored Contributor

Re: Date manipulation

Thanks Duncan, How can I use this in my script?
a warrior never quits
Steven E. Protter
Exalted Contributor

Re: Date manipulation

Shalom,

Read the script.

There is commentary inside that says exactly how to use it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Date manipulation

Hi:

If it's simply yesterday that you want to derive (in whatever format you desire), use a simple Perl script:

# perl -MPOSIX -le 'print strftime("%b %02d",localtime(time-24*60*60))'

Apr 19

You can use any of the 'date' formatting directives in place of what I have shown.

For example if you wanted a MM/DD/YYYY format:

# perl -MPOSIX -le 'print strftime("%m/%02d/%Y",localtime(time-24*60*60))'

04/19/2009

Regards!

...JRF...
Jeeshan
Honored Contributor

Re: Date manipulation

Thanks James for your input. But my concern is different.

Suppose I want to show the yesterday or day before yesterday date, and my current date is the first of month. How can I manipulate that the previous date was the last of past month?
a warrior never quits
Arturo Galbiati
Esteemed Contributor

Re: Date manipulation

hi,
do you need the last day of teh previous month or yesterday?
Please, specify.
Rgds,
Art
James R. Ferguson
Acclaimed Contributor

Re: Date manipulation

HI (again) Ahsan:

> Suppose I want to show the yesterday or day before yesterday date, and my current date is the first of month.

The Perl snippet I offered handles month and/or year changes too. With a minor modification, we can make the code report any number of days in the past or the future. Once again, you can use any of the 'date(1)' or 'strftime(3C)' formatting directives you see in their manpages.

# perl -MPOSIX -le '$d=shift||1;print strftime("%b %02d",localtime(time-$d*24*60*60))'

...yields yesterday as we have seen. NOtice that I didn't pass any argument. I could have passed "1" for one-day. Passing an argument of "0" would simply return today.

# perl -MPOSIX -le '$d=shift||1;print strftime("%b %02d",localtime(time-$d*24*60*60))' 2

...gives you the date two days ago.

perl -MPOSIX -le '$d=shift||1;print strftime("%b %02d",localtime(time-$d*24*60*60))' -- -1

...gives you tomorrow. NOtice that the '--' says that what follows isn't a switch, but an argument.

Regards!

...JRF...
Jeeshan
Honored Contributor

Re: Date manipulation

Thanks for your valuable suggestions.

I have a perl script which read date from a file and convert it like "yyyymmdd". But problem is if its read the date like "Fri Jul 13 04:44:01 2009" then its output is "20090713" whether when it reads the date like
"Sat Jul 4 12:54:22 2009" then it doesn't gives any output. The code is like

$ cat filter.pl
#!/usr/bin/perl -nl
BEGIN{
%m=(Jan=>1,Feb=>2,Mar=>3,Apr=>4,May=>5,Jun=>6,
Jul=>7,Aug=>8,Sep=>9,Oct=>10,Nov=>11,Dec=>12)};
m{^Event.+(\w{3})\s(\d\d|\d).+\s(\d{4})} and
printf "%4d%02d%02d\n",$3,$m{$1},$2;
1;

how can i solve this?
a warrior never quits