【发布时间】:2011-07-30 11:14:27
【问题描述】:
我如何获取一个 Apache 通用日志文件并在一个整洁的直方图中列出其中的所有 URL,例如:
/favicon.ico ##
/manual/mod/mod_autoindex.html #
/ruby/faq/Windows/ ##
/ruby/faq/Windows/index.html #
/ruby/faq/Windows/RubyonRails #
/ruby/rubymain.html #
/robots.txt ########
测试文件示例:
65.54.188.137 - - [03/Sep/2006:03:50:20 -0400] "GET /~longa/geomed/ppa/doc/localg/localg.htm HTTP/1.0" 200 24834
65.54.188.137 - - [03/Sep/2006:03:50:32 -0400] "GET /~longa/geomed/modules/sv/scen1.html HTTP/1.0" 200 1919
65.54.188.137 - - [03/Sep/2006:03:53:51 -0400] "GET /~longa/xlispstat/code/statistics/introstat/axis/code/axisDens.lsp HTTP/1.0" 200 15962
65.54.188.137 - - [03/Sep/2006:04:03:03 -0400] "GET /~longa/geomed/modules/cluster/lab/nm.pop HTTP/1.0" 200 66302
65.54.188.137 - - [03/Sep/2006:04:11:15 -0400] "GET /~longa/geomed/data/france/names.txt HTTP/1.0" 200 20706
74.129.13.176 - - [03/Sep/2006:04:14:35 -0400] "GET /~jbyoder/ambiguouslyyours/ambig.rss HTTP/1.1" 304 -
这就是我现在所拥有的(但我不确定如何制作直方图):
...
---
$apache_line = /\A(?<ip_address>\S+) \S+ \S+ \[(?<time>[^\]]+)\] "(?<method>GET|POST) (?<url>\S+) \S+?" (?<status>\d+) (?<bytes>\S+)/
$parts = apache_line.match(file)
$p parts[:ip_address], parts[:status], parts[:method], parts[:url]
def get_url(file)
hits = Hash.new {|h,k| h[k]=0}
File.read(file).to_a.each do |line|
while $p parts[:url]
if k = k
h[k]+=1
puts "%-15s %s" % [k,'#'*h[k]]
end
end
end
...
---
这是完整的问题:http://pastebin.com/GRPS6cTZ 伪代码很好。
【问题讨论】:
-
现在,仅仅因为你删除了显示这是一个家庭作业问题的帖子并不意味着你可以通过声称它不是来解决这个问题。
-
Phrogz,我从未声称这不是家庭作业。您能否将直方图添加到我的标签中,因为我不能?
-
@에이바 由于您使用
homework标签提出问题,然后将其删除并询问没有该标签的较小版本,我粗鲁地假设您试图隐藏这个事实。我道歉。有关创建直方图的帮助,请参阅我的答案。