基础篇-脚本题(2)
从老男孩老师那里抓的题:处理以下文件内容,将域名取出并进行计数排序,如处理:(百度和sohu面试题)
oldboy.log http://www.etiantian.org/index.html http://www.etiantian.org/1.html http://post.etiantian.org/index.html http://mp3.etiantian.org/index.html http://www.etiantian.org/3.html http://post.etiantian.org/2.htmlshell实现方式awk -F "/" '{print $3}' oldboy.log | sort -r | uniq -c
cut -d "/" -f3 oldboy.log | sort -r | uniq -c
cat oldboy.log | sed 's/^ http:\/\///g' | sed 's/\/.*$//g' | sort -r | uniq -c
以上三种实现方式比较简单
awk -F "/" '{++S[$3]} END {for(key in S) print key,S[key]}' oldboy.log|sort -k2第四种详解:
python实现:
# coding: utf-8
import sys
from itertools import groupby
#ListFile = sys.argv[1]
def demo(ListFile):
reList = []
files = file(ListFile,'r')
lines = files.readlines()
for item in lines:
#print item,
rLIst = item.split("/")
r = rLIst[2]
reList.append(r)
result = [(a,len(list(b))) for a,b in groupby(sorted(reList))]
return result
if __name__ == "__main__":
#demo(ListFile)
print demo("/tmp/oldboy.log")
- shell脚本练习基础篇2
- 1.shell脚本编程——基础篇(一)
- linux脚本基础篇--21条入门学习笔记
- Linux环境下Shell脚本基础篇-鸡兔同笼问题
- Bash 脚本:正则表达式基础篇 | Linux 中国
- Python实现自动挂机脚本(基础篇)
- Shell脚本基础篇
- 基础篇-脚本题(2)
- 工程脚本插件方案 - c集成Python基础篇
- Bash 脚本:正则表达式基础篇
- python基础篇--python脚本头
- Bat 脚本学习 (基础篇)
- 基础篇-脚本题(3)
- 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!
- 【COCOS2DX-LUA 脚本开发之一】LUA语言基础在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途
- 转载一个简单的lua展示【COCOS2DX-LUA 脚本开发之一】LUA语言基础在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!
- 详解shell脚本(一)——基础篇上
- 【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!
- Linux下ORCLE数据库增量备份脚本 (基础篇)
- 详解shell脚本(二)——基础篇中