我编写了一个python脚本来修剪两行之间的日志文件。
这是我写的:
import optparse
import datetime
parser = optparse.OptionParser()
parser.add_option("-f","--file",dest="log_file",
action="store",help="Specify log file to be parsed")
options, args = parser.parse_args()
vLogFile=options.log_file
start_time = raw_input("Please enter start time:\n[Format: HH:MM]=")
end_time = raw_input("Please enter end time:\n[Format: HH:MM]=")
trim_time = datetime.datetime.now().strftime('%d%H%M%S')
output_file = 'trimmed_log_%s.txt' %trim_time
with open(vLogFile) as file:
for vline in file:
vDate = vline[0:10]
break
start_line = vDate + ' ' + start_time
end_line = vDate + ' ' +end_time
print("Start time:%s" %start_line)
print("End time:%s" %end_line)
for num, line in enumerate(file, 1):
if line.startswith(start_line):
start_line_number = num
break
for num, line in enumerate(file, 1):
if line.startswith(end_line):
end_line_number = num
break
file.close()
print(start_line_number,end_line_number)
with open(vLogFile,"r") as file:
oFile = open(output_file,'a')
for num, line in enumerate(file, 1):
if num >= start_line_number and num <= end_line_number:
oFile.write(line)
print("%s Created" %output_file)
以下是脚本的结果:
$ python trim.py -f ErrorLog.txt
Please enter start time:
[Format: HH:MM]=16:16
Please enter end time:
[Format: HH:MM]=16:29
Start time:2017-11-12 16:16
End time:2017-11-12 16:29
(333, 2084)
trimmed_log_23063222.txt Created
此处起始行(333)正确但结束行(2084)不正确。
这是我的log file:
有人可以帮我吗?
谢谢, 约杰什
答案 0 :(得分:2)
这适用于itertools.dropwhile()
和itertools.takewhile()
:
import itertools
from datetime import datetime
start_time = datetime.strptime("16:16", "%H:%M")
end_time = datetime.strptime("16:29", "%H:%M")
with open('ErrorLog.txt') as f_log, open('trimmed.txt', 'w') as f_trimmed:
for row in itertools.dropwhile(lambda x: datetime.strptime(x[11:16], "%H:%M") < start_time, f_log):
f_trimmed.write(row)
break
for row in itertools.takewhile(lambda x: datetime.strptime(x[11:16], "%H:%M") < end_time, f_log):
f_trimmed.write(row)
这将为您提供输出trimmed.txt
,如下所示:
2017-11-12 16:16:16.642 Info: Forest Extensions state changed from open to start closing because shutting down
2017-11-12 16:16:16.642 Info: Database Extensions is offline
2017-11-12 16:16:16.643 Info: Forest Extensions state changed from start closing to middle closing because shutting down
.
.
2017-11-12 16:24:07.161 Info: Deleted 1 MB at 345 MB/sec /Users/yogeshjadhav96/Library/Application Support/MarkLogic/Data/Forests/App-Services/000001db
2017-11-12 16:24:07.165 Info: Deleted 10 MB at 2361 MB/sec /Users/yogeshjadhav96/Library/Application Support/MarkLogic/Data/Forests/App-Services/000001dc
这具有滤除不匹配起始要求的行的效果,即太早,然后仅读取行直到结束要求。读入每个row
并使用lambda函数提取时间,将其转换为datetime
对象,并相应地与start_time
或end_time
进行比较。
答案 1 :(得分:1)
问题是您在没有倒带的情况下对打开的文件进行枚举,因此行号不再正确。你可以使用input_file.seek(0)
来做到这一点,但有更简单的方法。这样的东西可能对主循环起作用(干编码,YMMV) - 此外,它只能读取文件一次。
with open(vLogFile) as input_file, open(output_file, 'a') as output_file:
do_write = False
for i, line in enumerate(file, 1):
if i == 1: # First line, so figure out the start/end markers
vDate = vline[0:10]
start_line = vDate + ' ' + start_time
end_line = vDate + ' ' +end_time
if not do_write and line.startswith(start_line): # If we need to start copying...
do_write = True
print('Starting to write from line %d', i)
if do_write:
output_file.write(line)
if line.startswith(end_line): # Stop writing, we have everything
print('Stopping write on line %d', i)
break