我在 Windows 2003 Server here 上找到了一个 IIS 日志文件的示例。不过,请发布您自己的示例日志行。
192.168.114.201, -, 03/20/01, 7:55:20, W3SVC2, SERVER, 172.21.13.45, 4502, 163, 3223, 200, 0, GET, /DeptLogo.gif, -,
由于这只不过是一个逗号分隔的文件,因此您有几种不同的方法可以访问这里。如果你的机器上安装了Text::CSV,你可以使用它。如果没有,这里有一个简单的例子。
use strict;
use warnings;
use Data::Dumper;
my $user = {}; # we will store the actions in here
# This is what the log file looks like when split into an array
# 0: Client IP address
# 1: User name
# 2: Date
# 3: Time
# 4: Service and instance
# 5: Server name
# 6: Server IP
# 7: Time taken
# 8: Client bytes sent
# 9: Server bytes sent
# 10: Service status code
# 11: Windows status code
# 12: Request type
# 13: Target of operation
# 14: Parameters
open $log, '<', 'path/to/logfile.log';
while (my $line = <$log>) {
my @fields = split /, /, $line; # split on comma and space
# you'll get an array of actions for each user
push @{ $user->{$fields[1]} }, "$fields[12] $fields[13]";
# or more specific:
# push @{ $user->{$fields[1]} }, {
# 'time' => $fields[3],
# 'action' => $fields[12],
# 'target' => $fields[13],
# };
}
close $log;
print Dumper $user; # lets have a look
# More stuff to do with the data here...
这是输出:
$VAR1 = {
'-' => [
'GET /DeptLogo.gif'
]
};
然后您可以将$user 的内容写入另一个文件或文件组。
foreach my $u (sort keys %$user) {
print "$u\r\n";
foreach $action (@{ $user->{$u} }) {
print "$action\r\n";
}
}