在我的代码中,我在一堆日志文件中搜索一些信息,这些日志文件每15分钟生成一次(时间部分不是那么重要),并且它给了我一定的输出,如下所示。但那不是我想要的。
我的最终目标是计算输出,这意味着我正在尝试将这一串数字转换为变量并计算每个餐厅的总数,然后我想要计算所有餐厅的总数。
老实说,我需要跟踪这些数字并计算出我需要的结果。我对目前的情况感到困惑,所以我需要帮助!
我希望我明白我的担忧。
示例日志文件:
Total number = 0 for Mcdonalds .....somelog.java:(000)
Total number = 5 for Mcdonalds .....somelog.java:(000)
Total number = 12 for Burger King.....somelog.java:(000)
Total number = 2 for Culvers .....somelog.java:(000)
Total number = 2 for Mcdonalds.....somelog.java:(000)
Total number = 19 for culvers.....somelog.java:(000)
Total number = 0 for Mcdonalds .....somelog.java:(000)
Total number = 0 for Mcdonalds .....somelog.java:(000)
Total number = 0 for Mcdonalds .....somelog.java:(000)
Total number = 19 for culvers.....somelog.java:(000)
Total number = 0 for culvers.....somelog.java:(000)
Total number = 5 for Mcdonalds .....somelog.java:(000)
Total number = 12 for Burger King.....somelog.java:(000)
Total number = 2 for Culvers .....somelog.java:(000)
Total number = 2 for Mcdonalds.....somelog.java:(000)
Total number = 19 for culvers.....somelog.java:(000)
Total Number for Mcdonalds = 7
Total Number for Culvers = 21
Total Number for BK = 12
Total for all = 40
#!/usr/bin/perl
use strict;
use warnings;
use File::Find::Rule;
use Date::Parse;
my ($dir, $type, $fh, $line, $str_1,
$str_2 );
$dir = '/dir/test/';
$type = '*';
$str_1 = 'Total Number= 0';
$str_2 = '(java:000)';
my @files = File::Find::Rule->file()->name($type)->in($dir);
open $out, '>>', "output_log" or die "Unable to open 'output_log' : $!";
print $out "\Logs \n";
print $out "--------------------------\n";
close $out or die "Unable to finish writing output_log : $!";
for my $file (@files) {
open $fh, '<', $file or die "can't open $file: $!";
open $out, '>>', "output_log" or die "Unable to open 'output_log' : $!";
while ( $line = <$fh> ) {
# Here I am searching for those which are not equal to zero
#and which has the tail java:(000)
if ( $line !~ /$str_1/ && $line =~ /$str_2/ ) {
print $out $line; #So here my output lines are all which are not zero.
}
}
close $out or die "Unable to finish writing output_log : $!";
}
答案 0 :(得分:-1)
我假设您正在获得如图所示的输出。所以我只是专注于将它们统一起来的部分
#!/usr/bin/perl
use strict;
use warnings;
my $string = "Total number = 5 for Mcdonalds
Total number = 12 for Burger King
Total number = 2 for Culvers
Total number = 2 for Mcdonalds
Total number = 19 for culvers";
my @rows = split("\n",$string);
my %hash = ();
foreach my $row (@rows) {
if($row =~ /(\d+)\s+for\s+(.*)$/i) {
my $comp = lc($2);
if(exists($hash{$comp})) {
$hash{$comp} = int($hash{$comp}) + int($1);
} else {
$hash{$comp} = int($1);
}
}
}
foreach (keys(%hash)) {
print $_ . "...." . $hash{$_} . "\n";
}