Skip to content

Instantly share code, notes, and snippets.

@IceDragon200
Forked from anonymous/gist:ff35732891c8608ce364
Last active August 29, 2015 14:10
Show Gist options
  • Save IceDragon200/d2567f01e5d3d3530999 to your computer and use it in GitHub Desktop.
Save IceDragon200/d2567f01e5d3d3530999 to your computer and use it in GitHub Desktop.
def parse_ip_string(line)
line.scan(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/).each do |addr|
split_array = addr.split(".")
return split_array.map{ |str| str.rjust(3, '0') }.join '.'
end
nil
end
unless ARGV.size == 1
puts "Invalid number of arguments?"
else
filename = ARGV[0]
ip_hash = Hash.new(0)
File.open(filename, 'r') do |in_file|
in_file.each_line do |line|
ip_string = parse_ip_string(line)
if ip_hash.key?(ip_string) # the ip_string was found
ip_hash[ip_string] += 1
else
ip_hash[ip_string] = ip_hash.default
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment