Skip to content

Instantly share code, notes, and snippets.

@manonthemat
Created November 18, 2013 03:48
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 manonthemat/7522153 to your computer and use it in GitHub Desktop.
Save manonthemat/7522153 to your computer and use it in GitHub Desktop.
function to sort a shell history file and print out the lines of commands that has been executed the most
def sortCommands(histFile, limit=10)
file = open(histFile, 'r')
lines = file.readlines
file.close
freqs = Hash.new(0)
lines.each do |line|
freqs[line] += 1
end
a = freqs.sort_by {|k, v| -v}[0..limit]
i = 0
limit.times do
puts a[i][0]
i += 1
end
end
sortCommands('.histfile')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment