Perl使用数组散列的散列计算每秒的日志条目数

时间:2012-02-21 10:49:05

标签: perl hash count logfile

更新

在我最初的帖子和回复后,我设法得到了另一个裂缝,并且写出了我的目标和结果更清晰:

目的:

我正在尝试计算日志文件的搜索字符串中的匹配数,以确定以下列方式生成消息的次数:

  • 每天总计。
  • 每小时总计。
  • 每小时最高,每小时。
  • 每小时最高,每小时。

我的工作代码:

#!/usr/bin/perl
#use strict;
use warnings;
use Data::Dumper;

my @a =  (  
    [ qw /2012-02-21_09:43:43/ ],
    [ qw /2012-02-21_09:43:43/ ],
    [ qw /2012-02-21_09:43:44/ ],
    [ qw /2012-02-21_09:43:44/ ],
    [ qw /2012-02-21_09:43:44/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:43:45/ ],
    [ qw /2012-02-21_09:44:47/ ],
    [ qw /2012-02-21_09:44:47/ ],
    [ qw /2012-02-22_09:44:49/ ],
    [ qw /2012-02-21_10:44:49/ ]
);

my ( %count, $count ) = ();

foreach (@a) {
    my $line = @$_[0] ;
    $line =~ /(\S+)_(\d+):(\d+):(\d+)/ ;

    my $day = $1;
    my $hour= $2;
    my $min = $3;
    my $sec = $4;

    $count {$day}->{$hour}->{$min}->{$sec}{'sec'} += 1 ;
    $count {$day}->{$hour}->{$min}{'min'} += 1 ;
    $count {$day}->{$hour}{'hour'} += 1 ;
    $count {$day}{'day'}  += 1 ;
}

#print Dumper (%count) . "\n";

foreach my $k1 ( sort keys %count ) {
    print "$k1\t$count{$k1}{'day'}\n" ;

    foreach my $k2 ( sort keys %{$count{$k1}} ) {
        if ($k2 =~ /day/) {
            next;
        }
        print " $k2:00\t\t$count{$k1}{$k2}->{'hour'}\n";

        foreach my $k3 ( sort keys %{$count{$k1}{$k2}} ) {
            if ($k3 =~ /hour/) {
                next;
            }
            print "  $k2:$k3\t\t$count{$k1}{$k2}{$k3}->{'min'}\n";

            foreach my $k4 ( sort keys %{$count{$k1}{$k2}{$k3}} ) {
                if ($k4 =~ /min/) {
                    next;
                }
                print "   $k2:$k3:$k4\t$count{$k1}{$k2}{$k3}{$k4}->{'sec'}\n";              
            }
            print "\n";
        }
        print "\n";
    }
}
exit;

结果

由于糟糕的哈希解除引用方法,我不得不关闭严格(我感到羞耻)。

2012-02-21  12
 09:00      11
  09:43     9
   09:43:43 2
   09:43:44 3
   09:43:45 4

  09:44     2
   09:44:47 2

 10:00      1
  10:44     1
   10:44:49 1

试图输出:

2012-02-21  12
 09:00      11
  09:43     9
   09:43:45 4   

 10:00      1
  10:44     1
   10:44:49 1

的问题:

  1. 是否有更好的编写代码的方法,并启用严格的?
  2. 如何列出哈希值中最高出现的哈希值,以尝试仅列出最高数量?
  3. 感谢以前的所有帖子,如果没有它们,我就无法实现这一目标。

    干杯,

    安迪

3 个答案:

答案 0 :(得分:1)

可以稍微简化(我也做了一些风格上的改变以提高可读性):

my @data =  (
    [ qw /2012-02-21_09:43:43/ ],
    [ qw /2012-02-21_09:43:43/ ]
);
my %counts;   
foreach my $words (@data) {
    my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ );
    $counts{$day}->{$hour} += 1;
}
foreach my $day (keys %counts) {
    foreach my $hour (keys %{ $counts{$day} }) { 
        print "Hour count for $day:$hour is: $counts{$day}->{$hour}\n";
    }
}

循环的工作部分是您的查询的核心:

    my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ );

    # You don't need minutes/seconds, so don't match them
    # On the other hand, it's better to match YYYY/MM/DD explicitly!
    # A regexp match in a list context will return a list of captures! 
    #     e.g. ($1, $2, ...)

    $counts{$day}->{$hour} += 1;
    # You need to merely add 1 to a value. No need to push ones on a list.

    # Please note that if the data is not guaranteed to be perfectly formatted, 
    # you need to defend against non-matches:
    $counts{$day}->{$hour} += 1 if (defined $day && defined $hour);

以下是相同的代码,其中添加了评论,阐明了我为何进行了风格修改:

my @data =  (  # Don't use @a - variable name should have meanings
    [ qw /2012-02-21_09:43:43/ ], # Not sure why you are using an array ref with
    [ qw /2012-02-21_09:43:43/ ], #   just 1 element, but let's pretend that is OK
);
my %counts;   
foreach my $words (@data) { # Almost never rely on $_ - less readable
    my ($day, $hour) = ($words->[0] =~ /(\d{4}-\d{2}-\d{2})_(\d+):/ ;
    $counts{$day}->{$hour} += 1; # You can omit "->" but that's less readable
}
foreach my $day (keys %counts) { # Always localize your variable to the block they need
    foreach my $hour (keys %{ $counts{$day} }) { 
        print "Hour count for $day:$hour is: $counts{$day}->{$hour}\n";
    }
}

答案 1 :(得分:1)

您应该考虑使用模块来解析时间戳,例如DateTime::Format::Strptime

use DateTime::Format::Strptime;

my $strp = new DateTime::Format::Strptime( 
    pattern => "%Y-%m-%d_%H:%M:%S" 
);

my $t = $strp->parse_datetime("2012-02-21_09:43:43"); 

my $year  = $t->year;
my $month = $t->month;
my $day   = $t->day;
# ...etc

如果您要做类似的事情:

for my $aref (@a) {
    for my $line (@$aref) {         # Note: better than $line = @$_[0]
        my $t = $strp->parse_datetime($line);
        my $key = sprintf "%s-%s", $t->year, $t->month;
        push @{$count{$key}}, $t;   # save the whole object in the array
    }
}

for my $key (sort keys %count) {
    my $count = @{$count{$key}};    # get size of array
    for my $obj (@{$count{$key}}) { # list all the DateTime objects
        my $hour  = $obj->hour;
        # etc ...
    }
}

您可以将时间戳中的所有数据存储到DateTime对象中,并在以后根据需要使用它。

答案 2 :(得分:0)

您的正则表达式存在问题以获取日期。 由于日期包含角色 - 你不能用\ d +获得整个日期 相反,您应该使用\ S +以便获得整个日期。 我现在正在尝试您的代码...将使用更多信息进行更新

  

更新1

     

我假设您想要获得每天和每小时的计数。所以稍微调整一下逻辑

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my ( @a, $line, %count, $day, $hour, $min, $sec ) = ();

@a =  ( 
[ qw /2012-02-21_09:43:43/ ],
[ qw /2012-02-21_09:43:43/ ],
[ qw /2012-02-21_09:43:44/ ],
[ qw /2012-02-21_09:43:44/ ],
[ qw /2012-02-21_09:43:44/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:43:45/ ],
[ qw /2012-02-21_09:43:47/ ],
[ qw /2012-02-21_09:43:47/ ],
[ qw /2012-02-21_09:43:49/ ],
[ qw /2012-02-21_10:43:49/ ],
);

foreach (@a) {
    $line = @$_[0] ;
    $line =~ /(\S+)_(\d+):(\d+):(\d+)/ ;

    $day    = $1;
    $hour   = $2;
    $min    = $3;
    $sec    = $4;

    #$count{$day} += 1;
    $count{$day}{$hour} += 1;
}

#print "Val is:".$count{$day}{$hour}."\n";

print Dumper (%count) . "\n";
foreach $day(keys%count)
{
    #print "Day count $day is:".$count{$day}."\n";
    foreach $hour(keys %{ $count{$day} })
    {
        print "Hour count $hour is:".$count{$day}{$hour}."\n";
    }
}