
zsh_history_sort.py
from collections import Counter, defaultdict
import sys
try:
    #FILENAME = "/Users/{hoge}/.zhistory"
    FILENAME = sys.argv[1]
except:
    print "USAGE: zsh_history_sort.py <your_history_file>"
    sys.exit(1)
class Stat(object):
    def __init__(self):
        self.counter = Counter()
        self.children = defaultdict(Stat)
stat = Stat()
fi = file(FILENAME)
for line in fi:
    line = line.strip()
    if ";" in line:
        line = line.split(";")[1]
    words = line.split()
    s = stat
    for (i, w) in enumerate(words):
        if i > 1: break;
        s.counter[w] += 1
        s = s.children[w]
def show(stat, indent=0):
    INDENT = "  " * indent
    for name, count in stat.counter.most_common():
        if count < 10: break
        print "%s%s: %d" % (INDENT, name, count)
        show(stat.children[name], indent + 1)
show(stat)
You can change the depth and the number of displays by changing ʻif i> 1: break; or ʻif count <10: break.
For commands that you use unexpectedly, you may want to consider abbreviated names.
↓ 90% 9 minutes 9 ㎘ The following sources are used. Former story show stat. of your git usage — Gist
Recommended Posts