Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图从一个常见的日志格式日志文件中获取三条信息。日志文件的条目将是:
65.54.188.137 - - [03/Oct/2007:02:20:22 -0400] "GET /~longa/statistics/code/xlispstat/smoothers/spline/ HTTP/2.0" 301 2633
然后,我想将 IP、URL 和状态代码的出现次数存储在哈希中。我想他们每个人都必须在他们自己的。任何帮助将不胜感激,即使您可以指出我正确的方向。
您可以使用正则表达式从日志条目中读取信息。像这样的东西:
lines.each do |line| matches = /^(\S+).*GET\s(.*)\sHTTP\S*\s(\d+)/.match(line) ip = matches[1] url = matches[2] status = matches[3] do
然后,您可以将此信息放入哈希中并按照您的喜好进行处理。