我正在嘗試在Perl中編寫日誌解析器。我想輸出所有用戶打印頁數的清單。解析Windows日誌文件以提取用戶名和打印在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
我相信答案是:是的,可能。 – TLP
編輯代碼以刪除查找並連接到輸入文件路徑的命令。 @TLP指出它們是不必要的。 –
你不應該在問題中修復你的代碼,這使得問題和答案看起來很傻。 – TLP