从日志文件中搜索值和计数

时间:2015-06-14 11:43:17

标签: linux bash perl shell unix

我在日志文件中的价值如下所示,我希望捕获所有相同的数字并计算它们并每隔10分钟放入另一个文件。我们怎么能这样做?

[14/06/2015 14:33:55.311] - WARNING- Conflict detected between two  sources !!! id1=67 id2=69 number=1193046
[14/06/2015 14:33:55.607] - WARNING- Conflict detected between two  sources !!! id1=70 id2=69 number=1193046
[14/06/2015 14:33:55.886] - WARNING- Conflict detected between two  sources !!! id1=69 id2=70 number=466000000
[14/06/2015 14:33:56.086] - WARNING- Conflict detected between two  sources !!! id1=64 id2=69 number=1193046
[14/06/2015 14:33:57.064] - WARNING- Conflict detected between two  sources !!! id1=70 id2=69 number=1193046
[14/06/2015 14:33:57.074] - WARNING- Conflict detected between two  sources !!! id1=64 id2=69 number=1193046
[14/06/2015 14:33:57.454] - WARNING- Conflict detected between two  sources !!! id1=68 id2=70 number=466000000
[14/06/2015 14:33:57.657] - WARNING- Conflict detected between two  sources !!! id1=68 id2=70 number=466000000
[14/06/2015 14:33:57.657] - WARNING- Conflict detected between two  sources !!! id1=68 id2=70 number=466000000
[14/06/2015 14:33:58.309] - WARNING- Conflict detected between two  sources !!! id1=67 id2=70 number=466000000
[14/06/2015 14:33:58.610] - WARNING- Conflict detected between two  sources !!! id1=70 id2=69 number=1193046
[14/06/2015 14:33:58.814] - WARNING- Conflict detected between two  sources !!! id1=68 id2=69 number=1193046
[14/06/2015 14:33:58.874] - WARNING- Conflict detected between two  sources !!! id1=69 id2=70 number=466000000

4 个答案:

答案 0 :(得分:2)

假设Data::Dump的输出格式足够好,这对Perl来说是一个微不足道的问题

perl -MData::Dump -e"/number=(\d+)/ and ++$c{$1} while <>; dd \%c;"  logfile.log

<强>输出

{ 1193046 => 7, 466000000 => 6 }

如果(无论出于什么原因)你不能安装Data::Dump那么输出对于核心模块Data::Dumper

来说是不够整洁
perl -MData::Dumper -e"/number=(\d+)/ and ++$c{$1} while <>; print Dumper \%c;"  logfile.log

<强>输出

$VAR1 = {
          '1193046' => 7,
          '466000000' => 6
        };

答案 1 :(得分:1)

在一个while循环中唤醒单行:

while true; do 
    awk '{a[$NF]++}END{for(t in a){printf "%d\t%s\n", a[t], gensub(/number=/,"",1,t)}}' logfile
    sleep 600
done

输出:

6       466000000
7       1193046

答案 2 :(得分:0)

此perl脚本会每10分钟在新文件(&#39; outfile。file number。txt&#39;)中打印出您的号码和计数

use warnings;
use strict; 

my %hash;
my $count;
while (1) {
    open my $in, '<', 'in.txt' or die $!;
    $count++;
        while(<$in>){
            chomp;
            my ($number) = /number=(\d+)/;
            $hash{$number}++;
        }
    open my $out, '>', "outfile.$count.txt" or die $!;
    print $out "$_\t$hash{$_}\n" foreach keys %hash;
    sleep 600;
}

答案 3 :(得分:-1)

我会做这样的事情:

cat log | grep -o "number\=[0-9]\+" | cut -d= -f2 | sort | uniq -c

您的示例的输出是:

7 1193046
6 466000000