Skip to content

Instantly share code, notes, and snippets.

@trueheart78
Last active November 7, 2022 19:57
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 trueheart78/def70430637042098a16061d3992f26d to your computer and use it in GitHub Desktop.
Save trueheart78/def70430637042098a16061d3992f26d to your computer and use it in GitHub Desktop.
A Z Shell History Parser - Now a git repo! https://github.com/trueheart78/zsh-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
Copy link
Author

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