今天在论坛看到一个问题就记了下来,分别用sed和python写了答案,当是re模块的小练习吧
2014-11-05 09:39:48: ip[192.168.10.152] logId[281424076]
2014-11-05 09:58:33: ip[192.168.10.152] logId[393876725]
2014-11-05 09:58:33: ip[192.168.10.152] logId[393883438]
2014-11-05 09:58:34: ip[192.168.10.152] logId[394052520]
2014-11-05 09:58:34: ip[192.168.10.152] logId[394057519]
第2列去掉最后一个冒号, 第3列和第4列取方括号里面的内容
sed:
sed ‘s/: / /;s/ip\[\(.*\)\]/\1/;s/\].*\[/ /‘ test
python:
#!/usr/bin/env python import re file = open(‘test‘) done = 0 while not done: line = file.readline() if line != ‘‘: line = line.strip(‘\n‘) this_line_list = line.split(‘ ‘) zz1 = re.compile(‘[0-9]{2}:[0-9]{2}:[0-9]{2}‘) zz2 = re.compile(‘[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}‘) zz3 = re.compile(‘[0-9]{1,}‘) pp1 = re.findall(zz1,this_line_list[1]) pp2 = re.findall(zz2,this_line_list[2]) pp3 = re.findall(zz3,this_line_list[3]) print ‘%s %s %s %s‘ % (this_line_list[0],pp1[0],pp2[0],pp3[0]) else: done = 1 file.close()
时间: 2024-09-30 14:54:22