在 Perl 中如何将多个日志文件视为一个虚拟文件?
我在日志目录中有多个访问日志,遵循以下命名约定:
access.log.1284642120
access.log.1284687600
access.log.1284843260
基本上,日志由 Apache 每天“轮换”,因此可以按顺序对它们进行排序。
我试图“依次阅读它们”,以便将它们视为一个日志文件。
my @logs = glob('logs/access.log.*');
上面的代码将全局所有日志,但我不确定:
- 日志将按字母顺序组织什么顺序?
- 如果我想查看“唯一IP的最新访问时间”,我该怎么做?
我有一个 Perl 脚本,可以读取单个访问日志并轻松检查(我的算法是有一个大哈希,它使用 IP 地址作为键,访问时间作为值,然后继续将键/值对推送到它...)。 但我不想只是为了这一过程而将所有访问文件合并到一个临时文件中。
有什么建议吗?非常感谢。
I've got multiple access logs in the logs directory, following the naming convention below:
access.log.1284642120
access.log.1284687600
access.log.1284843260
Basically, the logs are "rotated" by Apache per day, so they can be sorted in order.
I am trying to "read them one after another", so that they can be treated as one log file.
my @logs = glob('logs/access.log.*');
The above code will glob all the logs, but I am not sure:
- In which order will the logs be organized, alphabetically?
- if I want to check "the latest access time from an unique IP", how could I do this?
I have a Perl script that can read a single access log and check this easily (my algorithm is to have a big hash which uses IP address as the key and the access time as the value, and just keep pushing key/value pairs to it...).
But I don't want to just merge all access files into one temporary file just for this process.
Any suggestions? Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想确保特定的顺序,请自己对其进行排序,即使只是为了确保结果正确:
在这种情况下,除了特定数字外,文件名都相同,您可能不需要排序块:
要将它们作为一个 über 文件读取,我喜欢使用
local @ARGV
这样我就可以使用菱形运算符,这实际上就是神奇的ARGV
文件句柄。当到达@ARGV
中一个文件的末尾时,它会转到下一个文件。这通过在程序内分配给@ARGV
来伪造在命令行上指定所有文件:如果您需要知道当前正在处理的文件,请查看
$ARGV
。如果您需要更奇特的东西,您可能不得不诉诸蛮力。
If you want to ensure a particular order, sort it yourself, even if just to assure yourself that it will come out right:
In this case, where the filenames are all the same except for the particular digits, you might not need the sort block:
To read them as one über-file, I like to use a
local @ARGV
so I can use the diamond operator, which is really just the magicARGV
filehandle. When it gets to the end of one file in@ARGV
, it moves on to the next. This fakes specifying all the files on the command line by assigning to@ARGV
inside the program:If you need to know the file you are currently processing, look in
$ARGV
.If you need something more fancy, you might have to resort to brute force.
在 Unix-y 环境中,您可以利用 shell 将文件分组在一起:
In a Unix-y environment, you can leverage the shell to group your files together: