Parse apache common log format log files

2019-09-04 10:45发布

I'm trying to take three pieces of information from a common log format log file. An entry of the log file would be:

65.54.188.137 - - [03/Oct/2007:02:20:22 -0400] "GET /~longa/statistics/code/xlispstat/smoothers/spline/ HTTP/2.0" 301 2633

and from that, I want to store the number of occurrences of the IP, the URLs, and the status codes in a hash. I figured they each have to be in their own. Any help would be appreciated, even if you can just point me in the right direction.

标签: ruby parsing
1条回答
神经病院院长
2楼-- · 2019-09-04 11:37

You can read the information from the log entries with a regular expression. Something like this:

lines.each do |line|
  matches = /^(\S+).*GET\s(.*)\sHTTP\S*\s(\d+)/.match(line)
  ip = matches[1]
  url = matches[2]
  status = matches[3]
do

Then you can put this information into a hash and process it how you like.

查看更多
登录 后发表回答