在转换日期时间用awk数据管道纪元 [英] Convert datetime in to epoch using awk piped data
问题描述
我已经有日期和时间格式制表符分隔的日志文件2011-07-20 11点34分52秒的前两列:
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
我试图日期时间只用awk来划时代转换:
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}'
因此,这让我有些办法的,因为它让我划时代减少三分之二000对结束 - 但我刚开始系统命令输出 - 在那里,因为我真的想替换为$ 1划时代时间
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 output of the system command - where as i really want to substitute $1 with the epoch time.
我的目标以下的输出:
<epoch time>|$5|$10|$11|$12
我试着只是用:
cat logfile.log | grep 1010.gif | awk '{ print d };' "d=$(date +%s -d"$1")"
但是,这只是给了我空白行。
But this just gives me blank rows.
任何想法。
感谢
推荐答案
此假设呆子 - 不能做任何时区转换虽然,严格当地时间
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
}
'
这篇关于在转换日期时间用awk数据管道纪元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!