Python脚本从日志文本文件中提取特定字符串和相关时间戳w.r.t字符串

时间:2013-10-23 12:04:17

标签: python

我有一个名为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

我不知道那么多蟒蛇 - 有谁可以帮我这个?

@iblazevic

如上面的情况我有更多的要求..所以我尝试了一些代码。日志与上面的相同。

现在输出:如果字符串存在于log.txt中,那么它应该打印相应的TimeStamp  或者如果字符串不在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')

1 个答案:

答案 0 :(得分:1)

这是让你入门的东西,你可以继续努力并改进它:

with open("file.txt","r") as f:
    read_data = f.read()
    for line in read_data.splitlines():
        if "eth0: link up" in line:
            temp = line.replace('[','').split(']')
            print "eth0: link up  {0} {1}".format(temp[0], temp[1])

编辑:

嗨,这是你的问题的编辑,希望它有所帮助:

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(']')
                #changed line below
                print "{0:20}{1:30}{2:20}".format(ikey['pattern'],temp[0], temp[1])
            else:
                print "{0:50} {1:30}".format(ikey['pattern'],'NOT FOUND')