Hi all,
When I run the script I got this error message.
undefined method `[]' for nil:NilClass (NoMethodError)
from chandra.rb:11:in `collect'
from chandra.rb:11
# --------------8<---------------------------------8<---------------
require 'rubygems'record = parser.parse(line)
require 'apachelogregex'
require 'pp'
format = '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
parser = ApacheLogRegex.new(format)
access_log = Hash.new
# Creating a hash of an array of hashes!
# Can be simplified based on your actual requirement.
File.readlines('aleurier_access.log').collect do |line|
# The code below does the actual job. Can be a one-lineryes exactly this is what I want to do.
# if you know ternary operators or maybe if expression.
if access_log.key? record["%h"]
access_log[record["%h"]] << record
else
access_log[record["%h"]] = Array.new
end
end
# Now let's test some!
puts access_log.keys
while true
print "Enter ip address: "
ipaddr = gets.chomp
puts "-" * 40
pp access_log[ipaddr]
puts "-" * 40
end
# --------------8<---------------------------------8<---------------
Is this what you wanted ?