我正在尝试在 Perl 中编写日志解析器。我想输出所有用户打印页数的列表。
日志导出为制表符分隔的文本文件。有几列信息,但所有重要的信息都在最后一列。重要的部分如下所示:
Document 34, Microsoft Word - Q5_Springc_2013 owned by USERNAME on COMPUTERHOSTNAME was printed on PRINTER through port PORT. Size in bytes: 42096. Pages printed: 4. No user action is required.
#!/usr/bin/perl
use warnings;
#use strict;
print "Export the \"Printed to...\" logs from Event Viewer for the desired printer as a .txt and place it in the same directory as this script!\n";
print "Enter the text file name: ";
my $infile = <STDIN>;
if ($infile eq "\n"){
print "No filename entered! Exiting!";
exit 0;
}
chomp $infile;
print "Reading from file $infile\n";
open INFILE, "<$infile" or die "File does not exist!";
my %report;
while(<INFILE>){
if (/ by (\S+) on .* printed: (\d+)/s) {
$report{$1} += $2;
}
}
print "$_ $report{$_}\n" for (keys %report);
close INFILE or die $!;
我试图从用户名数组中提取唯一的名称并计算打印,但我没有找到比这更多的父亲。如果键存在但没有任何运气,我尝试改用哈希并通过将下一个值添加到旧值来使用键/值方案。谁能帮我弄清楚从这里去哪里?
我忘了提,我要的输出是这样的:
USER 45
USER2 12
USER3 120