over 1 year ago
Count (group by) number of line occurrences in file
Here's a neat ruby script to group and count the number of occurrences of lines inside a given file.
count.rb
list = IO.readlines(ARGV[0])
h = Hash.new {|hash, key| hash[key] = 0}
list.each {|item| h[item] += 1}
h = h.sortby { |k,v| v }.reverse
h.eachwithindex do |p,i|
puts "(#{p[1]}) #{p[0]}"
if ARGV[1] and ARGV[1].toi <= i + 1
break
end
end
Usage is:
ruby count.rb <file> [showatmostn]If you have a file named “faren.txt” comprised of the following lines:
test test awesome dude dude dude faren meranRunning the script with:
ruby count.rb faren.txtWill yield the following result:
(3) dude (2) test (1) awesome (1) faren (1) meranIn addition, if you pass the “showatmostn” parameter (a number), it will only print that number of results. For example 2 will show the following:
(3) dude (2) test