循环遍历数组而不是每次都加载数据集

时间:2014-08-28 20:57:52

标签: arrays perl

在下面的代码中,我试图在每次访问我的数组中的一个URL时搜索。这个脚本有效...但是因为我正在加载,所以有不必要的开销 " external.access_logs"对于for循环中的每次迭代。

我也希望从每个URL匹配的日志行中提取IP地址,下面是一个示例日志行,任何指导都将不胜感激。

2014-08-28      16:19:36        GET    /login/do.jsp 200     -       "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36"     -       727e13230b5dadbab1c0b6eda77dcf613ba5120e80dbaff753ff8e14e3d36aae501e3a004aeba73e238a50e36bc3c5922491f01cd433de30663ddeb2fb09d023c742a9e5e16f5b4eb08b213d"  -       -       -       "45.25.25.64"

剧本:

#!/usr/bin/perl
use warnings;
use strict;

my $date1 = `date -d "-1 day" +"%y-%m-%d"`;
chomp $date1;

my @array = ( "/login/do.jsp", "/logout/do.jsp", "/query/do.jsp" );

print "# of times resource was used, resource URL\n";

for (@array) {
    chomp $_;
    my $cmd = `cat /weblogs/daily_summaries/$date1/external.access_logs |awk {'print   \$4'} |grep -i "$_" |wc -l`;
    chomp $cmd;
    print "$cmd,$_\n";
}
print "Complete!\n";

1 个答案:

答案 0 :(得分:0)

解析日志文件一次以构建计数:

#!/usr/bin/perl
use strict;
use warnings;
use autodie;

my $date1 = `date -d "-1 day" +"%y-%m-%d"`;
chomp $date1;

my %count;

open my $fh, '<', "/weblogs/daily_summaries/$date1/external.access_logs";
while (<>) {
    my $file = (split)[3] // next;
    $count{$file}++;
}

my @array = ( "/login/do.jsp", "/logout/do.jsp", "/query/do.jsp" );

print "# of times resource was used, resource URL\n";

for (@array) {
    print "$count{$_},$_\n";
}
print "Complete!\n";