2007-05-20

* Ruby: Apacheログファイルから日毎のアクセス数を計測するスクリプト

いや、別にこんなのいらんけど。。。

#!/usr/bin/ruby -w
# USAGE: script logfile...

access_table = Hash.new

# analyze access count
ARGV.each do |file|
  File.foreach(file) {|line|
    adate = line[/\d{2}\/\w{3}\/\w{4}/]
    if access_table.key?(adate)
      access_table[adate] += 1
    else
      access_table[adate] = 1
    end
  }
end

# check invalid records
if access_table.key?(nil)
  access_table["Invalid_records"] = access_table.delete(nil)
end

# show result
print "INPUT: "
puts ARGV.join(", ")
sorted_table = access_table.sort_by{|key, value| key}
sorted_table.each{|key, value|
  puts "#{key} => #{value}"
}
Posted at 21:30 in | WriteBacks (0) | Edit