Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using awk to compare dates

Status
Not open for further replies.

KarstenJHilton

IS-IT--Management
Dec 24, 2006
9
US
I have been looking for 2 days for a solution for this, and now I am hoping someone with master awk skills can guide me in the way of what I am trying to do. :)

Problem:
I have a file that has dates in it, and I need to only see those that are less than, or equal to the system's date(Current Date). the format of the date in the file is DD Mon YYYY (30 Aug 2012). If I cat test_file, I see this output.

03 Oct 2012
03 Feb 2012
03 Jun 2012
29 Aug 2012
03 Oct 2012
03 Oct 2012
03 Aug 2012
30 Aug 2012
30 Aug 2012

Now if I run cat test_file | awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash
I get this this output, which is what I think I am needing to do the <= comparison.

20121003
20120203
20120603
20120829
20121003
20121003
20120803
20120830
20120830

In my simple test script, here is what I have.

#!/bin/ksh
dt=$(date +"%Y%d%m")
fil='cat test_date_file'
$fil|awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash

You can see I have the date command pulling the system date in the yyyymmdd to match the awked date.

So, how would I pull the specific dates I am wanting that are <= to current date.

Any help would be appreciated, and thanks in advance.

Thanks,

Karsten
 
I'd use the getline function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hey PHV,
Thanks for the heads up. I am not familiar with the getline. I reading up on it now. Do you have an example I could use that could help?

Thanks,
Karsten
 
In your awk man page, have a look at cmd | getline var

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The direction for this has changed. I am needing to only get everything that is 5 weeks, or older. I have been hitting a brick wall with this. I am trying to go in a different direction, any help would be appreciated. The file has the format as dd Mon YYYY (20 Sep 2012). I have drawn a blank. I am thinking of maybe a way to set each month as a numeric value. Jan=1, Feb=2, and etc.... Then have a call where if the value is less than current month then it could execute a command. But, then the problem comes into play where if I sit on the first day of a new month, then it gets the day before on back which won't work. I think I have dug so deep I have made this more complicated than it has to be. Thanks in advance for any clarity, and guidance.

 
Try this perhaps?

Code:
awk '
        [green]BEGIN[/green] {
                cmd=[red]"[/red][purple]date -d \"5 weeks ago\" +%Y%m%d[/purple][red]"[/red]
                cmd | [b]getline[/b] fiveweeksago
                [b]close[/b](cmd)
        }
        {
                cmd=[red]"[/red][purple]date -d \"[/purple][red]"[/red][blue]$0[/blue][red]"[/red][purple]\" +%Y%m%d[/purple][red]"[/red]
                cmd | [b]getline[/b] d
                [b]close[/b](cmd)
                [olive]if[/olive] (d > fiveweeksago) [b]print[/b]
        }
'


Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Hey PHV and Annihilannic,
Thanks for your help on this. I was able to get the script completed. I used something similar to Annihilannic's piece. Anyway, thank you all very much! I can take a nap now. [bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top