bash脚本也能实现,现在用python3重写了下这个脚本,以后肯定用的上,直接看我的实践例子,用的是我博客的访问记录进行筛选的,直接上代码了:
01.#!/usr/bin/python302.#coding:utf-803.l = []04.logs = {}05.with open('sulao.cn-access.log', 'r', encoding='utf-8') as f:06. while True:07. line = f.readline()08. line = line.strip('\n')09. rows = line.split(' ')10. #添加每行数据到列表11. l.append(rows)12. if not line:13. break14.#将列表第一列添加到字典并判断包含在字典内则值+1,否则值设置115.for x in l:16. if x[0] in logs:17. logs[x[0]] += 118. else:19. logs[x[0]] = 120.#使用zip将字典键和值打包成元祖列表21.f = zip(logs.values(), logs.keys())22.#reverse参数用来排序,true倒序,false倒序23.r = sorted(f,reverse=True)24.#循环打印前10的元祖25.for p in r[:10]:26. print(p)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://sulao.cn/post/608
评论列表