AWK epoch diff与当前和上一行

时间:2012-04-25 17:44:03

标签: linux bash awk

我有一个名为Sample的文件: -

206,,,206,14.9,0,2012/04/24 00:00:05
206,,,206,14.9,0,2012/04/24 00:00:21
205,,,205,14.9,0,2012/04/24 00:00:23
205,,,205,14.9,0,2012/04/24 00:00:29
207,,,207,14.9,0,2012/04/24 00:00:34
205,,,205,14.9,0,2012/04/24 00:00:40
204,,,204,14.9,0,2012/04/24 00:00:46
202,,,202,14.9,0,2012/04/24 00:00:52
201,,,201,14.9,0,2012/04/24 00:01:00
202,,,202,14.9,0,2012/04/24 00:01:04

以下AWK命令: -

awk -F, '{ gsub("/"," ",$7); gsub(":"," ",$7); t+=(mktime($7)-mktime(p)); printf ("%s,%s,%s\n",mktime($7),mktime(p),t); p=$7 }' Sample

给出以下输出: -

1335222005,-1,1335222006
1335222021,1335222005,1335222022
1335222023,1335222021,1335222024
1335222029,1335222023,1335222030
1335222034,1335222029,1335222035
1335222040,1335222034,1335222041
1335222046,1335222040,1335222047
1335222052,1335222046,1335222053
1335222060,1335222052,1335222061
1335222064,1335222060,1335222065

对于每一行,第7列将转换为纪元日期,并计算上一行上的纪元日期之间的差异,并将其添加到t

在正在处理的第一行,由于p目前不是日期,因此mktime会返回-1抛出我的数据。

我想要做的是,告诉AWK脚本,如果正在处理第1行,则假设差值为6.目前它从1335222005减去-1,结果为1335222006。

我想说,从6开始t,然后在第二行开始,计算出与上一行相差的纪元秒数,并按该数量增加t

3 个答案:

答案 0 :(得分:3)

你只需要为第1行做一些特别的事。

awk -F, '
    {gsub(/[\/:]/," ",$7); this_time = mktime($7)}
    NR != 1 {t += this_time - prev_time; print this_time, prev_time, t}
    {prev_time = this_time}
' << END

根据输入数据,打印

1335240021 1335240005 16
1335240023 1335240021 18
1335240029 1335240023 24
1335240034 1335240029 29
1335240040 1335240034 35
1335240046 1335240040 41
1335240052 1335240046 47
1335240060 1335240052 55
1335240064 1335240060 59

或者,初始化变量的一种简便方法是使用awk'f -v选项

awk -v t=6 '... same as before ...'

答案 1 :(得分:2)

awk中,您可以初始化BEGIN块中的变量,并存在两个变量来获取行号,这两个变量对您的案例都很有用,FNRNR

BEGIN { t = 6 }

FNR == 1 { t = 6 }

答案 2 :(得分:1)

使用BEGINsee here)会有帮助吗? 这将允许将t变量初始化为您想要的任何内容。像

这样的东西
  awk -F, 'BEGIN {t=6} { gsub("/"," ",$7); gsub(":"," ",$7); t+=(mktime($7)-mktime(p)); printf ("%s,%s,%s\n",mktime($7),mktime(p),t); p=$7 }' Sample