Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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