#!/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