Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Z Shell History Parser
#!/usr/bin/env ruby
require 'etc'
commands = {}
File.readlines(File.join(Etc.getpwuid.dir, '.zsh_history')).each do |l|
l = l.encode('utf-8', 'binary', :undef => :replace)
next if l == ''
next unless l.match(/: \d+:/)
l = l.sub(';','[SPLIT]').split('[SPLIT]')
key = l.last.split.first
commands[key] = 0 unless commands.key? key
commands[key] += 1
end
most_used = commands.reject { |k, v| v < 10 || k.size < 4 || k.start_with?('exe/') || k.start_with?('scripts/') || k.start_with?('script/') || k.start_with?('bin') || k.start_with?('./') }.map { |k, v| [v, k]}.sort_by { |a| a.first }.reverse[0..20]
puts 'Top 20 most used commands'
most_used.each do |n, k|
puts "[#{k}] #{n}"
end
@trueheart78

This comment has been minimized.

Copy link
Owner Author

@trueheart78 trueheart78 commented Feb 27, 2018

Sample output

Top 20 most used commands
[docker] 222
[brew] 113
[brake] 100
[ruby] 81
[mkdir] 77
[curl] 70
[gcof] 58
[gcob] 49
[open] 49
[kitty] 43
[wget] 40
[echo] 38
[youtube-dl] 35
[bundle] 34
[docker-compose] 32
[bcop] 30
[pkill] 27
[dropbox-gif-linker] 27
[ytdl] 26
[heroku] 23
[alias] 23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment