12

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

Let's say we have below file:

boo,foo,2016-08-30
foo,boo,2016-07-31

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.

4 Answers 4

28

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

BEGIN {
    str = "date +%Y-%m-%d";
    str | getline date;
    close(str);
}
$3>date

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

gawk -F, 'BEGIN{date=strftime("%Y-%m-%d")}$3>date'
7
  • Why quotes (") around $( ) ? Commented Aug 23, 2015 at 18:43
  • 2
    @Archemar Habit mostly. It is not necessary if date's format does not have spaces. Commented Aug 23, 2015 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? Commented Aug 24, 2015 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. Commented Aug 24, 2015 at 7:12
  • 1
    You want an awk pattern of just $3>date (with empty=default action) no /.../ slashes Commented Aug 25, 2015 at 23:32
7

using the getline workaround:

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

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()}'
1440536144

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
  • 5
    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. Commented Aug 25, 2015 at 21:10
  • 7
    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. Commented Aug 25, 2015 at 21:14
3

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}}'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.