#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
def f(his, n):
lines = open(os.path.join(os.getenv('HOME'), his)).readlines()
d = {}
for line in lines:
line = line.split()
if not line: continue
s = line[0]
if line[0] == 'sudo':
s = line[1]
d[s] = d.get(s, 0) + 1
l = sorted(d.items(), key=lambda t: t[1], reverse=True)
for t in l[:n]:
print '%s : %d' % (t[0].ljust(16), t[1])
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-f', '--file', dest='f', default='.history',
help='the history file', metavar='FILE')
parser.add_option('-c', '--count', dest='c', type='int', default='30',
help='for display count', metavar='COUNT')
(options, args) = parser.parse_args()
f(options.f, options.c)
运行结果:
Shell:~ >: count_his_cmd
cd : 4895
ls : 4533
vi : 988
man : 679
hitfm : 583
cat : 532
ifconfig : 414
ant : 401
rm : 386
lt : 349
ll : 341
tmp : 340
svn : 333
tlist : 332
top : 311
my : 292
exit : 278
run : 277
unlcall : 249
pkg_list : 249
glib : 242
locate : 235
shutdown : 226
bbs.sh : 220
check : 216
ping : 215
pkill : 198
mv : 196
vpn : 190
sock : 186
Shell:~ >:
from http://code.google.com/p/mylibs/source/browse/trunk/tools/Python/count_history_cmd.py