伯爵在多个文件中的字段的唯一事件 [英] Count unique occurrences of a field across multiple files

查看:99
本文介绍了伯爵在多个文件中的字段的唯一事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收集了大量的日志文件,每个文件包含格式记录...

I have a large collection of log files where each file contains records of the form ...

2015-06-07 23:59:53 [uid:123] {success,1} .

对于每一个文件,我想指望许多独特的UID是如何present。

For each file, I want to count how many unique UIDs are present.

因此​​,在这个文件片段,我们看到的UID 123和124 ......

So in this file snippet we see the uids 123 and 124 ...

2015-06-07 23:59:53 [uid:123] {success,1}
2015-06-07 23:59:53 [uid:123] {success,1}
2015-06-07 23:59:53 [uid:123] {success,1}
2015-06-07 23:59:53 [uid:124] {success,1}

所以我算此文件的结果将是的 2

如何使用bash和/或AWK我得到的数据?

How can I get the data using bash and/or awk?

我试过

cat 20150607.log | awk '{print $3}' | sort | uniq | wc -l

这个效果不错,但问题是我有这么多的文件,我不希望运行上面的命令一个接一个。

This worked well, but the problem is I have so many files and I do not want to run the above command one by one.

有没有得到这个数翻过多个文件的简单的方法?

Is there a simpler way of getting this count accross multiple files?

推荐答案

使用GNU AWK为 ENDFILE 长度(阵列)

Using GNU awk for ENDFILE and length(array):

awk '{unq[$3]} ENDFILE{print FILENAME, length(unq); delete unq}' *.log

使用其他awks:

awk '
    !seen[FILENAME,$3]++ { unq[FILENAME]++ }
    END { for (i=1;i<ARGC;i++) print ARGV[i], unq[ARGV[i]]+0 }
' *.log

这篇关于伯爵在多个文件中的字段的唯一事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆