LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to convert past or future dates in awk (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-convert-past-or-future-dates-in-awk-796761/)

btacuso 03-20-2010 06:17 PM

how to convert past or future dates in awk
 
Hi,

All I see from googling examples in awk is converting current date. How do I convert a list of past or future dates for example :
Jan18'09
Aug 7'98
Jun20'11
Apr 1'10

I will be using it to compare dates in a file and act on it accordingly. Thanks again.

jlinkels 03-20-2010 09:20 PM

There is no such function in awk. Your best bet is to use the shell date command to convert you date to a time stamp and work with this time stamp for comparing.

This doesn't imply you can call the date command from within awk. You'd have to write a shell script to create, compare and process, and use awk as a helper for the rest. In other word, use awk to dissect your file and produce the different fields, and use shell to do something with that output.

I am sure the next poster in this thread will suggest to use Perl and then the next one Python, but if you don't speak those like me, almost everything can be done in shell/awk

jlinkels

grail 03-21-2010 02:15 AM

It would help if you also gave a demo of how you would like to modify your input? (ie what the finished result would be)

btacuso 03-21-2010 04:52 AM

Quote:

Originally Posted by grail (Post 3906267)
It would help if you also gave a demo of how you would like to modify your input? (ie what the finished result would be)

For example I want to change any date with the format "Mmmdd'YY" to
"mm/dd/yyyy" in any line in a file( ex. Jan18'09 changed to 01/18/2009.

grail 03-21-2010 05:16 AM

Fair enough. Well as jinkels said there is no specific awk converter, but you could create a
table (read array) or place in another file (ie jan=1, feb=2, ...) and then source it to get
your mappings. The other numbers should be fairly straight forward.

Let me know if that helps?

Edit: Sorry, forgot to add that you also need to make decisions for the year component too,
ie. Jan18'09 could be 01/18/2009 or 01/18/1909 or etc

colucix 03-21-2010 05:18 AM

The problem is just to translate the date in a format suitable to the date command. The more similar to your input is "mmmdd yyyy", so that something like:
Code:

date -f <(sed "s/'/ 20/" file) +%m/%d/%Y
should work, except for the fact you have to find a way to distinguish dates from the past century and from the current one.

Regarding awk, it has some functions to manage dates: using a combination of mktime and strftime it can manage dates in the past, but maybe it's far easier to stick with the date command.


All times are GMT -5. The time now is 12:38 PM.