【发布时间】:2015-03-18 06:09:23
【问题描述】:
我正在编写一个自动化脚本来提取与日志文件中第一个字符串 (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:”行获得成功消息。但是,如果我包含“其他”,即使第一个 if 通过,我也只会看到“文件未处理”。逻辑哪里错了?
例如: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
else被注释时,结果为:
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
【问题讨论】:
-
如果文件中的 first 行没有您的
inputName,您的for循环将停止,这真的是您所期望的吗? -
没有它遍历所有行并打印匹配行。但是,如果在日志文件中找不到任何 inputName,我需要仅为该特定 inputName 打印“文件未处理”。
-
请添加您的示例 test.log。代码看起来不错。
-
我不太理解
elif m: print "File not updated"(虽然我现在理解了问题的另一部分),那么应该在什么情况下打印呢? -
场景是检测到会有文件上传。文件可能会被检测到,但可能由于任何问题而无法上传。所以我想跟踪检测到和上传、检测到和未上传、未检测到的文件。以下是日志文件中的格式: 2015-03-18 06:34:24.820 INFO --- xxxxxxx :检测到 /tmp/test/abc-16 2015-03-18 06:36:44.030 INFO --- xxxxxxx : /tmp/test/abc-16 文件已上传
标签: python regex string-matching