Is there a constant variable in awk, that store today's date? if no, is there a way that can store today's date for daily use?

let's say we have below file:


And I need to compare the date $3 in the file, with today's date, regardless what it is.

i.e below script:

awk -F, '{if($3>"2015-08-23"){print $0}}'

Where 2015-08-23 will be changed by the current date.


There are no built-in functions in standard awk to get a date, but the date can easily be assigned to a variable.

awk -F, -v date="$(date +%Y-%m-%d)" '$3>date'

or in an awk script

    str = "date +%Y-%m-%d";
    str | getline date;
    close str;

gawk, does have built-in time functions, and strftime can be used.

gawk -F, 'BEGIN{date=strftime("%Y-%m-%d")}$3>date'
  • Why quotes (") around $( ) ? – Archemar Aug 23 '15 at 18:43
  • 1
    @Archemar Habit mostly. It is not necessary if date's format does not have spaces. – kdhp Aug 23 '15 at 19:06
  • @kdhp, the date format in this way will be for example 15-08-24 while for right comparison it should be 2015-08-24 is there a way to change it to this format? – Eng7 Aug 24 '15 at 5:56
  • 1
    @Azizieh7 The year should be correct, at least according to the standard. It might be that what should be an upper-case Y is, in fact, in the lower-case. As for changing the format, you can check your system's man date, or the standard linked above. – kdhp Aug 24 '15 at 7:12
  • 1
    You want an awk pattern of just $3>date (with empty=default action) no /.../ slashes – dave_thompson_085 Aug 25 '15 at 23:32

If you like to get the current time_t, this can be done by calling srand() and then call t = srand().

$ awk 'BEGIN{srand(); print srand()}'

This works as posix requires the random function to be initialized with the current time if called without arg and to return the previous seed.

  • 2
    Note that it works with many awk implementations and used to be the most portable way to get the current Unix epoch time at all (let alone in awk). But not all. All POSIX requires is that the seed be based on the time of the day (a pretty useless requirement IMO), not specifically the unix epoch time, which allows implementations to use something less predictable and that changes more often than every second. Now I can't remember which awk implementation that doesn't work on. – Stéphane Chazelas Aug 25 '15 at 21:10
  • 4
    Ah! Just found the thread I was vaguely remembering: groups.google.com/forum/#!topic/comp.unix.shell/ONhLIUW4CgU So it seems it won't work on OpenBSD. – Stéphane Chazelas Aug 25 '15 at 21:14

using the getline workaround:

awk -F, ' BEGIN { "date +%Y-%m-%d" | getline a } { if ( $3>a ) { print 0 }} ' file

You can do it by assigning a variable:

cat /path/to/file | awk -v date="$(date +'%Y-%m-%d')" -F, '{if($3>date){print $0}}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.