Created
July 22, 2019 18:25
-
-
Save sfluor/fd03c4534e9975c08e62692dc44f2686 to your computer and use it in GitHub Desktop.
Parse zsh history to see most used commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Usage: ./zsh_history.py <path_to_history_file> | |
import sys | |
import os | |
import pprint | |
from collections import defaultdict | |
from operator import itemgetter | |
commands = defaultdict(int) | |
zsh_history = os.path.realpath(os.path.expanduser("~/.zsh_history")) | |
if len(sys.argv) == 2: | |
zsh_history = sys.argv[1] | |
with open(zsh_history, "r") as f: | |
line = f.readline() | |
while line: | |
try: | |
line = f.readline() | |
commands[line.split(";")[1].split()[0]] += 1 | |
except UnicodeDecodeError: | |
continue | |
except IndexError: | |
continue | |
pprint.pprint(sorted(commands.items(), key=itemgetter(1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment