count_history_cmd.py

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值