【问题标题】:Perl count log entries per second using a hash of a hash of ararysPerl 使用数组散列的散列计数每秒的日志条目
【发布时间】:2017-07-14 11:45:42
【问题描述】:

更新:

在我最初的帖子和回复之后,我设法再次破解,并将我的目标和结果写得更清晰:

目标:

我正在尝试计算日志文件的搜索字符串中的命中次数,以了解通过以下方式生成了多少次消息:

  • 每天的总数。
  • 每小时总计。
  • 每分钟每小时最高。
  • 每秒最高,每小时。

我的工作代码:

#!/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;

结果

由于我的哈希解除引用方法不佳,我不得不关闭 strict(我很惭愧)。

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. 我将如何列出哈希值中出现次数最多的哈希值,以尝试仅列出最高计数?

感谢之前的所有帖子,没有他们,我不可能走到这一步。

干杯,

安迪

【问题讨论】:

  • 关闭严格以“解决”问题就像荷马辛普森在他的车里的“低油”警告灯上贴上一条胶带。它不会阻止问题,它只是隐藏它。
  • 这不是一个好的解决方案,但我不知道如何进行。
  • 你应该缩小问题的范围,尝试解决它,如果失败,请提出一个新问题。

标签: perl hash count logfile


【解决方案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);

这是添加了 cmets 的相同代码,阐明了我进行样式更改的原因:

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";
    }
}

【讨论】:

  • 原来是这样正确引用hash的方法,睡着的时候马上想到了!我忘记了加法运算符。谢谢,填补知识空白,一次一行。
【解决方案2】:

您应该考虑使用一个模块来解析您的时间戳,例如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 对象中,并在以后根据需要使用。

【讨论】:

  • 我不使用这个 Perl 实例管理系统,但该模块看起来确实是解析时间戳的最佳方法。谢谢,我会试一试的。
【解决方案3】:

获取日期的正则表达式有问题。 由于日期包含字符 - 您无法使用 \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";
    }
}

【讨论】:

  • 我使用了那个正则表达式,因为我只想获取当天的条目,但我认为你的更好。我想我会使用 cpan 模块并使用它,但感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 2014-05-19
  • 2017-02-21
  • 2010-12-31
  • 2016-08-03
  • 2014-01-16
相关资源
最近更新 更多