获取字段中值的总和

时间:2016-05-25 09:40:09

标签: bash date datetime sh

我有一个包含此类条目的文本文件:

SERVER02 |smsgateway0004 |0 |20160521 00:01:00.0
SERVER05 |smsgateway0001 |0 |20160521 00:01:00.0
SERVER03 |smsgateway1003 |0 |20160521 00:01:00.0
SERVER02 |smsgateway0002 |0 |20160521 00:02:00.0
SERVER05 |smsgateway0002 |0 |20160521 00:02:00.0
SERVER03 |smsgateway1002 |0 |20160521 00:02:00.0
SERVER02 |smsgateway1002 |0 |20160521 00:03:00.0
SERVER05 |smsgateway0004 |0 |20160521 00:03:00.0
SERVER03 |smsgateway0002 |0 |20160521 00:03:00.0
SERVER02 |smsgateway0001 |0 |20160521 00:04:00.0
SERVER05 |smsgateway0005 |0 |20160521 00:04:00.0
SERVER03 |smsgateway0001 |0 |20160521 00:04:00.0
SERVER02 |smsgateway0003 |0 |20160521 00:05:00.0
SERVER05 |smsgateway0003 |0 |20160521 00:05:00.0
SERVER03 |smsgateway0005 |0 |20160521 00:05:00.0
SERVER02 |smsgateway0005 |0 |20160521 00:06:00.0
SERVER06 |smsgateway0005 |0 |20160521 00:06:00.0
SERVER03 |smsgateway1001 |0 |20160521 00:06:00.0
SERVER02 |smsgateway1001 |0 |20160521 00:07:00.0
SERVER06 |smsgateway0003 |0 |20160521 00:07:00.0
SERVER03 |smsgateway0003 |0 |20160521 00:07:00.0
SERVER06 |smsgateway0004 |0 |20160521 00:07:00.0
SERVER03 |smsgateway0004 |0 |20160521 00:07:00.0

第一个字段是服务器名称,第二个字段是实例,第三个字段是计数,第四个字段是日期,最后一个是时间戳。

我想要做的是获取时间戳每分钟的第三列的总和。 (例如,所有服务器的00:02:00.0之和为4119)。

基本上,我想找到落在特定时间戳下的所有计数的总和。

Image

2 个答案:

答案 0 :(得分:1)

$ awk -F '[ |]+' '{arr[$5]+=$3} END{for(key in arr) print key, arr[key]}' data 
00:07:00.0 0
00:06:00.0 0
00:05:00.0 0
00:04:00.0 0
00:03:00.0 0
00:02:00.0 0
00:01:00.0 0

由于你的所有值都是0,我已经放了一些虚拟数据:

$ cat data 
SERVER02 |smsgateway0004 |2 |20160521 00:01:00.0
SERVER05 |smsgateway0001 |0 |20160521 00:01:00.0
SERVER03 |smsgateway1003 |5 |20160521 00:01:00.0
SERVER02 |smsgateway0002 |0 |20160521 00:02:00.0
SERVER05 |smsgateway0002 |0 |20160521 00:02:00.0
SERVER03 |smsgateway1002 |0 |20160521 00:02:00.0
SERVER02 |smsgateway1002 |0 |20160521 00:03:00.0
SERVER05 |smsgateway0004 |0 |20160521 00:03:00.0
SERVER03 |smsgateway0002 |0 |20160521 00:03:00.0
SERVER02 |smsgateway0001 |0 |20160521 00:04:00.0
SERVER05 |smsgateway0005 |0 |20160521 00:04:00.0
SERVER03 |smsgateway0001 |0 |20160521 00:04:00.0
SERVER02 |smsgateway0003 |6 |20160521 00:05:00.0
SERVER05 |smsgateway0003 |5 |20160521 00:05:00.0
SERVER03 |smsgateway0005 |0 |20160521 00:05:00.0
SERVER02 |smsgateway0005 |0 |20160521 00:06:00.0
SERVER06 |smsgateway0005 |0 |20160521 00:06:00.0
SERVER03 |smsgateway1001 |0 |20160521 00:06:00.0
SERVER02 |smsgateway1001 |0 |20160521 00:07:00.0
SERVER06 |smsgateway0003 |0 |20160521 00:07:00.0
SERVER03 |smsgateway0003 |0 |20160521 00:07:00.0
SERVER06 |smsgateway0004 |0 |20160521 00:07:00.0
SERVER03 |smsgateway0004 |0 |20160521 00:07:00.0
$ awk -F '[ |]+' '{arr[$5]+=$3} END{for(key in arr) print key, arr[key]}' data 
00:07:00.0 0
00:06:00.0 0
00:05:00.0 11
00:04:00.0 0
00:03:00.0 0
00:02:00.0 0
00:01:00.0 7

答案 1 :(得分:1)

我不太清楚我明白你想要什么,但我们走了:

awk '$5 ~ /00:07:00/ {sum += substr($3, 2)} END{print sum}' input_file

此代码将匹配第五个字段中00:07:00的所有行。然后拿第三个字段并将它们加在一起。

在您的示例输出中,所有内容都将为0,但输入为:

SERVER02 |smsgateway0005 |10|20160521 00:06:00.0
SERVER06 |smsgateway0005 |22|20160521 00:06:00.0
SERVER03 |smsgateway1001 |13|20160521 00:06:00.0
SERVER02 |smsgateway1001 |0 |20160521 00:07:00.0
SERVER06 |smsgateway0003 |11|20160521 00:07:00.0

00:07:00的输出为11,00:06:00

的输出为45