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 < 3 || 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

commented Feb 27, 2018

Sample output

Top 20 most used commands
[gam] 601
[git] 430
[cat] 203
[mkdir] 116
[curl] 111
[gaa] 99
[ruby] 99
[gcob] 95
[gpu] 90
[gem] 82
[rubocop] 67
[irb] 57
[brew] 57
[echo] 55
[bundle] 52
[pbcopy] 51
[tree] 51
[gpf] 46
[ssh] 45
[bcopfx] 42
[gcof] 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.