问:我试图在syslog(Linux服务器)中搜索特定事件的第一个条目,然后执行一个命令(将来的开发),然后记录信息。我不确定我是否处理得对。在
问题:不确定如何在找到第一个有效项时退出循环。在import os
import commands
from time import strftime
file = "server.log.1" # sample log
nlogFile =("/home/nsoper/log/logfile_%s.txt") %(strftime("%Y_%m_%d_ %H_%M_%S")) # create logfile
caption="SN" # sample serach
condition2 = "java error"
F=open(file, 'r')
nF=open(nlogFile, 'a')
nF.write("output for results search:\n\n")
print(strftime("%Y_%m_%d_ %H_%M_%S"))
for line in F:
for item in line.split("\n"):
if caption in item:
line1=item.strip()
if condition2 in line1:
print line1.strip()
nF.write(line1.strip()+"\n")
#command execute
nF.close()
F.close()