我有一个名为的文件log.txt
,其格式如下:
Wed Oct 16 16:48:55.322 2013] [ 23.923652] warning: `zygote' uses 32-bit capabilities (legacy support in use)
[Wed Oct 16 16:49:06.470 2013] [ 35.071554] lowmemorykiller: lowmem_shrink: convert oom_adj to oom_score_adj:
[Wed Oct 16 16:49:06.479 2013] [ 35.079038] lowmemorykiller: oom_adj 0 => oom_score_adj 0
[Wed Oct 16 16:49:06.479 2013] [ 35.084666] lowmemorykiller: oom_adj 1 => oom_score_adj 58
[Wed Oct 16 16:49:06.506 2013] [ 35.090390] lowmemorykiller: oom_adj 2 => oom_score_adj 117
[Wed Oct 16 16:49:06.506 2013] [ 35.096195] lowmemorykiller: oom_adj 4 => oom_score_adj 235
[Wed Oct 16 16:49:06.506 2013] [ 35.102037] lowmemorykiller: oom_adj 9 => oom_score_adj 529
[Wed Oct 16 16:49:06.506 2013] [ 35.107808] lowmemorykiller: oom_adj 15 => oom_score_adj 1000
[Wed Oct 16 16:49:06.753 2013] [ 35.356038] ADDRCONF(NETDEV_UP): eth0: link is not ready
[Wed Oct 16 16:49:08.505 2013] [ 37.099758] `eth0: link up`, 10 Mb/s, half duplex, flow control enabled
[Wed Oct 16 16:49:08.505 2013] [ 37.106453] eth0: link up, 100 Mb/s, full duplex, flow control enabled
现在我想从上面的值中提取字符串"eth0: link up"
、对应的时间戳"16:49:08.505"
和对应的值 "37.099758"
。
输出应如下所示:
String timestamp time spent
eth0: link up 16:49:08.505 37.099758
我不知道那么多python - 任何人都可以帮助我吗?
@iblazevic
由于上述情况我有一些更多的要求..所以我尝试了一些代码。日志将与上述相同。
现在输出:如果字符串存在于 log.txt 中,那么它应该打印相应的时间戳,或者如果字符串不在 log.txt 中,它应该显示没有找到字符串
String timestamp time spent
eth0: link up 16:49:08.505 37.099758
SPI Controller probe NOT FOUND
i2c /dev entries driver NOT FOUND
我尝试使用以下代码..但我得到了一些不同的输出。你能帮我解决这个问题吗?
#!/usr/bin/python
import argparse
import re
parser = argparse.ArgumentParser(description='Log file parse to check Boot time log')
parser.add_argument('-f','--logfile', help='logfile to filter', required=True)
args = parser.parse_args()
log_patterns = [
{'pattern':'sys_init start'},
{'pattern':'Image verified,start/Start Bootloader'} ,
{'pattern':'Kernel image decrypt'},
{'pattern':'verify Kernel image passed'},
{'pattern':'Start kernel at 0x01008000'},
{'pattern':'Uncompressing Linux... done, booting the kernel'},
{'pattern':'SPI Controller probe'},
{'pattern':'i2c /dev entries driver'},
{'pattern':'net eth0:'},
{'pattern':'Network_init/eth0: link becomes ready'},
{'pattern':'eth0: link up'},
]
with open(args.logfile,"r") as f:
read_data = f.read()
for line in read_data.splitlines():
for ikey in log_patterns:
if ikey['pattern'] in line:
match = re.search(ikey['pattern'], line)
if match:
temp = line.replace('[','').split(']')
print "{0:50}{1:30}".format(ikey['pattern'],temp[0], temp[1])
else:
print "{0:50} {1:30}".format(ikey['pattern'],'NOT FOUND')