我正在编写一个自动化脚本来提取与日志文件中第一个字符串(inputName)匹配的行,如果在该行中找到该特定匹配项,则在该特定行中搜索第二个字符串(successful_msg),其中显示“文件已上载成功”。
以下是代码:
import re
successful_msg="file has been uploaded"
def log_check(fileName):
search_fileName = fileName
print search_fileName
with open("/tmp/test.log") as line:
for match in line:
m=re.search(r"%s" %search_fileName, match)
n=re.search(r"%s" %successful_msg,match)
if m and n:
print match
elif m:
print "File not updated"
else:
print "File is not processed"
for inputName in glob.glob('./files/*'):
log_check(inputName)
我能够从“if m and n:”行获得成功的消息。但是,如果我包含“其他”,我只会看到“文件未处理”,即使第一个如果通过。逻辑错在哪里?
例如:ls files/
abc-15 abc-16 abc-123 gg
我想要的输出应该是:
abc-15
2015-03-17 06:09:26.122 INFO --- *** : The /tmp/test/abc-15 file has been uploaded
abc-16
2015-03-17 06:08:42.692 INFO --- *** : The /tmp/test/abc-16 file has been uploaded
gg
File is not processed
abc-123
File not updated
在循环中取消注释/考虑else时的实际结果是:
gg
File not updated
abc-15
File not updated
abc-16
File not updated
abc-123
File not updated
当评论其他时,结果是:
gg
abc-15
2015-03-17 06:09:26.122 INFO ---*** : The /tmp/test/abc-15 file has been uploaded
abc-16
2015-03-17 06:08:42.692 INFO --- *** : The /tmp/test/abc-16 file has been uploaded
abc-123
答案 0 :(得分:0)
我建议您对def
进行以下更改,并附上我的评论:
import re
successful_msg="file has been uploaded"
def log_check(fileName):
search_fileName = fileName
print search_fileName
with open("/tmp/test.log") as line:
# New variable to determine if fileName is matched or not
matched = 0
for match in line:
m = re.search(r"%s" %search_fileName, match)
# If fileName found, update above variable
if m:
matched = 1
n=re.search(r"%s" %successful_msg,match)
# On any line, if both phrases are present on the same line:
if n and m:
print match
break
# If for loop exited without break...
else:
# If we found the filename...
if matched == 1:
print "File not updated"
# If even the filename was not found...
else:
print "File is not processed"
for inputName in glob.glob('./files/*'):
log_check(inputName)