- 论坛徽章:
- 3
|
学生党:飘过~无聊刚起床~python实现一下
- $ cat yhsafe.py
- #!/usr/bin/env python
- #-*- coding:gbk -*-
- "like sed /awk pattern process file !~"
- import re
- import sys
- import linecache as lc
- list={}
- tm_match=''
- pattern=re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:')
- for line in lc.getlines(sys.argv[1]):
- ip_match=re.match(pattern,line)
- if ip_match:
- temp=line.strip()
- tm_match=''
- else:
- tm_match=' '.join([tm_match,line])
- list[temp]=tm_match
- print list
- for x in list:
- print x,list[x].replace('\n',' ')
复制代码 测试:
$ ./yhsafe.py yhsafe.txt
114.113.144.2: 19ms 19ms 19ms 36ms 22ms 19ms 18ms
218.61.204.73: 0ms 0ms 0ms 0ms 0ms 0ms 0ms
|
|