Re: Date manipulation (194 Views)
Reply
Honored Contributor
Jeeshan
Posts: 2,084
Registered: ‎11-05-2007
Message 1 of 9 (194 Views)
Accepted Solution

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
Honored Contributor
Duncan Edmonstone
Posts: 5,678
Registered: ‎08-05-2000
Message 2 of 9 (194 Views)

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

HTH

Duncan
Honored Contributor
Jeeshan
Posts: 2,084
Registered: ‎11-05-2007
Message 3 of 9 (194 Views)

Re: Date manipulation

Thanks Duncan, How can I use this in my script?
a warrior never quits
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 4 of 9 (194 Views)

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
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (194 Views)

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...
Honored Contributor
Jeeshan
Posts: 2,084
Registered: ‎11-05-2007
Message 6 of 9 (194 Views)

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
Esteemed Contributor
Arturo Galbiati
Posts: 830
Registered: ‎02-10-2003
Message 7 of 9 (194 Views)

Re: Date manipulation

hi,
do you need the last day of teh previous month or yesterday?
Please, specify.
Rgds,
Art
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 9 (194 Views)

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...
Honored Contributor
Jeeshan
Posts: 2,084
Registered: ‎11-05-2007
Message 9 of 9 (194 Views)

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
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.