Skip to content

Instantly share code, notes, and snippets.

@linyows
Created September 18, 2014 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linyows/a4f77b69e21c0a2d6824 to your computer and use it in GitHub Desktop.
Save linyows/a4f77b69e21c0a2d6824 to your computer and use it in GitHub Desktop.
CloudFrontのログからUAだけをsortして表示
#!/usr/bin/env ruby
log = File.read './E1ZEGDWBQZMV1Z.2014-09-16-13.hf802CqY'
array_log = log.split("\n").map { |v| v = v.split("\t") }
user_agents = array_log.map { |v| v[10][0..100] if v[10].is_a?(String) }.compact
ua_with_count = user_agents.each_with_object({}) do |ua, memo|
if memo.empty? || memo[ua].nil?
memo[ua] = 1
else
memo[ua] = memo[ua] + 1
end
end
puts "total: #{user_agents.count}"
ua_with_count.sort { |(_, v1), (_, v2)| v2 <=> v1 }.each do |v|
puts "#{"% 5d" % v[1]}, #{v[0]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment