Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Using awk to compare dates

Using awk to compare dates

Using awk to compare dates

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. smile

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.


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

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.



RE: Using awk to compare dates

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?


RE: Using awk to compare dates

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.

RE: Using awk to compare dates

Try this perhaps?

CODE --> awk

awk '
        BEGIN {
                cmd="date -d \"5 weeks ago\" +%Y%m%d"
                cmd | getline fiveweeksago
                cmd="date -d \""$0"\" +%Y%m%d"
                cmd | getline d
                if (d > fiveweeksago) print

tgmlify - code syntax highlighting for your tek-tips posts

RE: Using awk to compare dates

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close