六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

count_history_cmd.py

[复制链接]

升级  29.67%

89

主题

89

主题

89

主题

举人

Rank: 3Rank: 3

积分
289
 楼主| 发表于 2013-1-15 03:00:29 | 显示全部楼层 |阅读模式
#!/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
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表