convert date and time to epoch in awk

bw.mail.lists bw.mail.lists at gmail.com
Wed Feb 20 02:35:22 UTC 2013



 >> So, is there a way to compare two dates in FreeBSD's awk or convert 
a date
 >> to epoch? Or some other fast way to select the last 10 minutes from 
a log
 >> file? An example would be appreciated, if possible.
 >
 >
 > Converting a date to an epoch is easy with date(1) (note: awk can 
make a system
 > call and read back the stdout into a variable).
 >
 > For example, if I want to convert the date:
 >
 > Fri 01 Feb 2013
 >
 > into an epoch using:
 >
 > date -j -f "%a %d %b %Y" "Fri 01 Feb 2013" +%s
 >
 > The output of which is the following epoch:
 >
 > 1359763497
 >
 > Doing this all from awk:
 >
 > echo "Fri 01 Feb 2013" | awk '
 > {
 >     mydate = $0
 >     "date -j -f \"%a %d %b %Y\" \"" mydate "\" +%s" | getline myepoch
 >     print mydate " = " myepoch
 > }'
 >
 > Hope this helps.
 >

It does, exactly what I was looking for, many thanks.


More information about the freebsd-questions mailing list