LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-20-2010, 07:17 PM   #1
btacuso
Member
 
Registered: May 2009
Posts: 32

Rep: Reputation: 15
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.
 
Old 03-20-2010, 10:20 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,728

Rep: Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754
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
 
Old 03-21-2010, 03:15 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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)
 
Old 03-21-2010, 05:52 AM   #4
btacuso
Member
 
Registered: May 2009
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
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.
 
Old 03-21-2010, 06:16 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,565

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
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

Last edited by grail; 03-21-2010 at 06:20 AM. Reason: Year issue
 
Old 03-21-2010, 06:18 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Virtualizationís MF Future is in its MF Past LXer Syndicated Linux News 0 10-31-2008 08:20 AM
LXer: OOXML, the past. ODF, the future. LXer Syndicated Linux News 0 09-06-2007 12:30 PM
Using Awk to make Archives based on dates. HunterColt22 Linux - Newbie 4 05-29-2007 03:50 AM
LXer: Porky Vista Beta 2 Casts Doubt On Future Release Dates LXer Syndicated Linux News 0 06-17-2006 03:03 AM
Upgrading Debian - Past and Future... TheSwine Debian 4 01-05-2004 12:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration