Operating System - HP-UX
1847203 Members
3311 Online
110263 Solutions
New Discussion

Re: Converting date in a shell script

 
SOLVED
Go to solution
Timothy P. Jackson
Valued Contributor

Converting date in a shell script

I am developing a shell script that needs to accept multiple date formats and be able to convert them into the long date format.

Example:

10/29/07 ---> 29 October 2007
10-29-2007 ---> 29 October 2007
10.29.07 ---> 29 October 2007

etc.....

I need this so that I can grep out information from the certian log files. Because this is going to be run by normal users I am not sure if I can get them to enter the date the same time, every time.

Does any one have suggestions or recommendations on how to do this?

Thanks,
Tim
12 REPLIES 12
blah2blah
Frequent Advisor
Solution

Re: Converting date in a shell script

use Clay Stephenson's 'caljd' date manipulation utility!

You can download it from Merijn's website:

http://mirrors.develooper.com/hpux/
Timothy P. Jackson
Valued Contributor

Re: Converting date in a shell script

Thanks,

I down loaded it but I am have some diffulty with the syntax. Is there a "help" with Clay's utility?


Tim
blah2blah
Frequent Advisor

Re: Converting date in a shell script

either -u or -? will generate a usage message
Timothy P. Jackson
Valued Contributor

Re: Converting date in a shell script

Ok, I got the date to convert to a Julian Date, but now I need to convert it back a specific format of.....

Mon Oct 29 ... ... ...

this would be the same format that the "date" command would give.

Maybe there is an easier way to do this. I need to get all the entries out of the br_log file for the past 3 months, or from any give date forward.
blah2blah
Frequent Advisor

Re: Converting date in a shell script

just replying to return to top of queue.

maybe someone familiar with clay's date tool can provide further examples in how to do this
Timothy P. Jackson
Valued Contributor

Re: Converting date in a shell script

Thanks for following up on this. Clay's date converter utility works great. I spent some time with his examples and found what I needed. I also found that to create a script to pull this information out of the br_log file was going to take a lot longer than it was worth.

So I imported the br_log file into Excell and striped out the data that I didn't want. This should make management happy.

Thanks again!

Tim
Timothy P. Jackson
Valued Contributor

Re: Converting date in a shell script

See above reply
A. Clay Stephenson
Acclaimed Contributor

Re: Converting date in a shell script

This is really not a task for caljd.sh (or caljd.pl); your problem is tailor-made for the ParseDate function of the Perl Date::Manip module. Date::Manip is available from any CPAN site. ParseDate can recognize many different input formats and convert it to YYYYMMDDHH:MM:SS. Converting to another format is quite simple from that output.
If it ain't broke, I can fix that.
Wim Rombauts
Honored Contributor

Re: Converting date in a shell script

Although Clay's tool may be magnificent, I still wonder what is wrong with the date command :
date +"%d %B %E" return (TMHO) exactly what you look for.
Timothy P. Jackson
Valued Contributor

Re: Converting date in a shell script

Thanks for the information Clay.

When I have some more time I will persue this option. For now Excel will have to job. It is kind of funny that there has not been a utility to sort this information out already created.
A. Clay Stephenson
Acclaimed Contributor

Re: Converting date in a shell script

The thought of a poor defenseless UNIX box being forced to use Excel is more than my heart can bear. It took me all of about 3 minutes to throw together this Perl sample.

Since you changed your requirements from the 1st example to the 2nd example, I decided to output 2 lines for each input argument.

date.pl "10/29/07" "10-29-2007" "10.29.07" "Oct 10, 2007"

should all be handled with ease. You will need to install the Date::Manip module and that will take you about 4 minutes to download and install from www.perl.org/CPAN.

You should be able to take this and run with it even if you make your Perl script a blackbox that is called by a shell script.



If it ain't broke, I can fix that.
Timothy P. Jackson
Valued Contributor

Re: Converting date in a shell script

Thanks Clay, this is awesome!

I too was thinking the same thing as I imported the br_log file into Excel. Its a sad day when I have to use MS to do my work for me.

Thanks again for all your help.



Tim