I have a file that is a log file for web traffic. I would like to convert the timestamp in it to unix time or epoch time.
I am using the date command in conjunction with awk to try to do this. Just
myfile:
28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813
28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/catalog.gif 693
28/Aug/1995:00:00:39 1 /pub/atomicbk/catalog/laser.gif 129
28/Aug/1995:00:00:40 1 /pub/atomicbk/catalog/logo2.gif 12871
I can run the date command like this:
date -j -f "%d/%b/%Y:%T" 28/Aug/1995:00:00:38 +%s
And I get the intended result:
809593238
(809593230 seconds since Jan 1, 1970)
So myfile above, the expected output should be:
809593238 1 /pub/atomicbk/catalog/home.gif 813
809593238 1 /pub/atomicbk/catalog/catalog.gif 693
809593239 1 /pub/atomicbk/catalog/laser.gif 129
809593240 1 /pub/atomicbk/catalog/logo2.gif 12871
I'm close but I'm getting an error.
I run the following command:
cat myfile | awk '{ date -j -f "%d/%b/%Y:%T" print $1 echo +%s }'
The error I get is:
awk: syntax error at source line 1
context is
{ date -j -f "%d/%b/%Y:%T" >>> print <<< $1 echo " +%s" }
awk: illegal statement at source line 1
I've tried various combinations of this but with no success. I think it could be because 1) commands like date may not work inside an awk statement and 2) I need to properly escape the '+' and the '%' inside an awk statement so it can be passed on to the date command correctly. Any help is much appreciated.
Thanks!