1

从以这种格式输入后输出条目的文件中获取信息:IPAddress xx [date:time -x] "method url httpversion" statuscode bytes "referer" "useragent"

您将如何将该文件作为命令行参数访问并存储该信息,以便您可以按 IP 地址的字母顺序排列它,同时将所有信息放在一起?我假设我需要以某种方式使用哈希和数组。

理论上,您可以拥有任意数量的文本文件作为命令行参数,但到目前为止,我还没有让这部分工作,我只有:

./logprocess.pl monster.log #monster.log is the file that contains entries

然后在代码中,假设所有未指定的变量都被声明为标量

my $x = 0;
my @hashstuff;
my $importPage = $ARGV[0];
my @pageFile = `$importPage`;
foreach my $line (@pageFile)
{

    $ipaddy, $date, $time, $method, $url, $httpvers, $statuscode, $bytes, $referer, $useragent =~ m#(\d+.\d+.\d+.\d+) \S+ \S+ [(\d+/\S+/\d+):(\d+:\d+:\d+) \S+] "(\S+) (\S+) (\S+)" (\d+) (\d+) "(\S+)" "(\S+ \S+ \S+ \S+ \S+)"#
    %info = ('ipaddy' => $ipaddy, 'date' => $date, 'time' => $time, 'method' => $method, 'url' => $url, 'httpvers' => $httpvers, 'statuscode' => $statuscode, 'bytes' => $bytes, 'referer' => $referer, 'useragent' => $useragent);
    $hashstuff[$x] = %info;
    $x++;
}

肯定有更好的方法来做到这一点,因为我的编译器说我有全局符号错误,例如:

在 ./logprocess.pl 第 51 行 (#2) (W ambiguous)(S) 将 % 的歧义使用解析为运算符 % 您所说的内容可能无法按照您的想法进行解释。通常,通过提供缺少的引号、运算符、括号对或声明很容易消除歧义。

它不会执行。我不能使用任何模块。

4

1 回答 1

4

如果日志由 Apache 生成,您可以使用Apache::ParseLog模块。查看页面末尾的示例以获取灵感。

关于你提到的错误,你应该声明你的数组my

my @hashstuff;

并在那里添加参考。也可以通过以下方式访问单个项目$hashstuff[$x](注意开头的美元):

$hashstuff[$x] = { %info };

或者你可以完全摆脱$x

push @hashstuff, { %info };
于 2011-03-07T15:15:02.663 回答