开发者

Convert datetime in to epoch using awk piped data

I've a tab delimited log file that has date time in format '2011-07-20 11:34:52' in the first two columns:

An example line from the log file is:

2011-07-20  11:34:15    LHR3    1488    111.111.111.111 GET djq2eo454b45f.cloudfront.net    /1010.gif   200 -   Mozilla/5.0%20(Windows%20NT%206.1;%20rv:5.0)%20Gecko/20100101%20Firefox/5.0 T=F&Event=SD&MID=67&AID=dc37bcff-70ec-419a-ad43-b92d6092c9a2&VID=8&ACID=36&ENV=demo-2&E=&P=Carousel&C=3&V=3

I'm trying to convert the date time to epoch using just awk:

cat logfile.log | grep 1010.gif | \
awk '{ print $1" "$2" UTC|"$5"|"$10"|"$11"|"$12 }' | \
awk 'BEGIN {FS="|"};{system ("date -d \""$1"\" +%s" ) | getline myvar}'

So this gets me some way, in that it gets me epoch less three 000's on the end - however i'm just getting the o开发者_开发百科utput of the system command - where as i really want to substitute $1 with the epoch time.

I'm aiming for the following output:

<epoch time>|$5|$10|$11|$12

I've tried just using:

cat logfile.log | grep 1010.gif |  awk '{ print d };'  "d=$(date +%s -d"$1")"

But this just gives me blank rows.

Any thoughts.

Thanks


This assumes gawk -- can't do any timezone translation though, strictly local time.

... | gawk '
      BEGIN {OFS = "|"}
      {
          split($1, d, "-")
          split($2, t, ":")
          epoch = mktime(d[1] " " d[2] " " d[3] " " t[1] " " t[2] " " t[3])
          print epoch, $5, $10, $11, $12
      }
'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜