Skip to content

Instantly share code, notes, and snippets.

@pschwede
Last active October 12, 2015 18:57
Show Gist options
  • Save pschwede/4071846 to your computer and use it in GitHub Desktop.
Save pschwede/4071846 to your computer and use it in GitHub Desktop.
Unix command history distribution
#!/usr/bin/env python
from os.path import expanduser
from collections import Counter
distribution = Counter()
f = open(expanduser("~/.bash_history"))
commands = list()
for line in f.readlines():
line = line.replace("\n","").split(" ")
for i in range(1, len(line) + 1):
commands.append(" ".join(line[:i]))
f.close()
for command in commands:
distribution[command] += 1
print "Cnt:\tCmd:"
for cmd,cnt in sorted(distribution.items(), key=lambda x: x[1] * len(x[0])):
print "%s\t'%s'" % (cnt, cmd)
@squiddy
Copy link

squiddy commented Nov 14, 2012

Cool idea. If you can use Python2.7, collections.Counter can cut that code down a little.

from collections import Counter

distribution = Counter()

for command in commands:
    distribution[command] + 1

for cmd, cnt in distribution.most_common():
    print cnt, cmd

http://docs.python.org/2/library/collections.html#collections.Counter

@squiddy
Copy link

squiddy commented Nov 14, 2012

Of course you need to use += instead of +, my mistake.

@pschwede
Copy link
Author

Thanks, I made an update!

@pschwede
Copy link
Author

pschwede commented Oct 7, 2013

In bash:

# weighted sort by command length and count
history | awk '{ print $2 }' | sort | uniq -c | awk '{ print $1+$1+length($2), $1, $2 }' | sort -n | awk '{ print $2, $3 }'

# sort by command count
history | awk '{ print $2 }' | sort | uniq -c | sort -n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment