如何按日期对grep结果进行分组

时间:2014-11-05 19:00:57

标签: python perl unix awk grep

我在日志文件中有以下内容,

01:31:01,222收到的事件
01:31:01,435收到的活动
01:31:01,441收到的活动
01:31:01,587收到的活动
01:31:02,110收到的活动
01:31:02,650收到的活动
01:31:02,869收到的活动
01:31:03,034收到的活动
01:31:03,222收到活动

我想用秒对此进行分组,并计算每组中的行数以输出以下内容,

01:31:01 4
01:31:02 3
01:31:03 2

理想情况下,我喜欢在一个简单的awk脚本中执行此操作,而不必使用perl或python,任何想法?感谢。

2 个答案:

答案 0 :(得分:3)

听起来像是awk

的工作
awk -F, '{a[$1]++}END{for(i in a){print i, a[i]}}' file.txt

<强>输出:

01:31:01 4
01:31:02 3
01:31:03 2

<强>解释

我正在使用选项-F(字段分隔符)并将其设置为,。这使得在字段1($1)中获得秒精度的时间变得容易。

脚本本身的说明(以多行形式):

# Runs on every line and increments a count tied to the first field (the time)
# (The associative array a will get created on first access)
{a[$1]++}

# Runs after all lines have been processed. Iterates trough the array 'a' and prints
# each key (time) and its associated value (count)
END {
    for(i in a){
        print i, a[i]
    }
}

答案 1 :(得分:0)

如果您不关心输出订单,您可以这样做:

cut -d, -f1 file|uniq -c

(如果数据不是最初总是排序的话,在| uniq之前使用| sort)。

产地:

      4 01:31:01
      3 01:31:02
      2 01:31:03
相关问题