Skip to content

Instantly share code, notes, and snippets.

@sfluor
Created July 22, 2019 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfluor/fd03c4534e9975c08e62692dc44f2686 to your computer and use it in GitHub Desktop.
Save sfluor/fd03c4534e9975c08e62692dc44f2686 to your computer and use it in GitHub Desktop.
Parse zsh history to see most used commands
#!/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