[英]How to search for multiple strings in a file with multiline string
尝试编写一个脚本,搜索整个文件中的某些字符串。
超过2个字符串。
1)首先搜索以检查以下两行中的任何一行:
0/RP1/CPU0 RP(Active)
要么
0/RP0/CPU0 RP(Active)
如果' 0/RP1/CPU0 RP(Active)
'则打印此消息“ execute command location 0/rp1/cpu0
”
如果' 0/RP0/CPU0 RP(Active)
'则打印此消息“ execute command location 0/rp0/cpu0
”
2)第二次搜索是检查以下多行中的任何一行:a)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : ENABLED
b)
INFO_LINE------------------: TITLE_LINE(A-Z)
State : DISABLE
' TITLE_LINE(AZ)
'可能略有不同,但INFO_LINE
将是静态的,并且在ENABLED
或DISABLE
中都是相同的。
如果b)为真,则打印“ restart process on location (FROM SEARCH1)
。
我已经尝试过if/else/elif
语句并且一直在研究使用正则表达式的re.search。
#!/usr/bin/python
activerp = open('sample-output.txt')
def check_active_rp():
for line in activerp:
if line.find('0/RP1/CPU0 RP(Active)'):
print("execute command location 0/rp1/cpu0")
else:
if line.find('0/RP0/CPU0 RP(Active)'):
print("execute command location 0/rp0/cpu0")
运行这个脚本python只是让我回到cli提示符,我无法进一步完成其他搜索。
CLI $ python test.py CLI $
我想这就是你想要的:
def check_active_rp():
string = '0/RP1/CPU0 RP(Active)'
for line in activerp:
if string in line:
print('execute command location 0/rp1/cpu0')
我创建了一个包含你正在搜索的字符串并进行了一些测试的文件,你的例子应该给你一些输出,虽然错了。 它让我觉得你没有完全掌握python脚本,但如果我错了就纠正我。
为了执行您的功能,您需要调用它。 编写def
只是定义它。 你可以在这里找到更多相关信息。
我看到你正在看这个正则表达式,但如果你正在搜索的字符串没有变化,你可以使用find函数。
问题是line.find()
返回一个整数而不是一个布尔值。 所以你总是输入第一个if语句,除非你的行以'0/RP1/CPU0 RP(Active)'
开头(因为它会返回0索引)。 如果我们检查文档,我们可以看到如果没有找到字符串, find函数返回-1。 所以你可以用这样的东西改变你的if语句: line.find('0/RP1/CPU0 RP(Active)') != -1
。 多行字符串也可以这样做。 唯一的事情是你需要将整个文件转储到一个字符串中。 因此,考虑到这一点,这是可以解决问题的解决方案。
def check_active_rp(activerp):
whole_file = activerp.read()
if whole_file.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif whole_file.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
if whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)\n State : ENABLED') != -1:
print('state is ENABLED')
elif whole_file.find('INFO_LINE------------------: TITLE_LINE(A-Z)\n State : DISABLE') != -1:
print('restart process on location (FROM SEARCH1)')
with open('sample-output.txt') as active_rp:
check_active_rp(active_rp)
在您的示例中,您也永远不会关闭文件,因此我使用了with语句,这在处理IO时被认为是一种很好的做法。
更新:
我想你想改变信息行中的内容,在这种情况下使用正则表达式是合适的。 然后,以下解决方案将起作用:
import re
def check_active_rp(activerp):
iterator = iter(activerp)
for line in iterator:
if line.find('0/RP1/CPU0 RP(Active)') != -1:
print("execute command location 0/rp1/cpu0")
elif line.find('0/RP0/CPU0 RP(Active)') != -1:
print("execute command location 0/rp0/cpu0")
pattern = re.compile('INFO_LINE------------------: ([A-Z]+)')
x = pattern.search(line)
if x:
line = next(iterator)
if line.find('ENABLED') != -1:
print('the {} is ENABLED'.format(x.group(1)))
elif line.find('DISABLE') != -1:
print('the {} is DISABLED'.format(x.group(1)))
所以我们从文件中创建一个迭代器并开始逐行遍历文件。 我们仍然使用字符串查找功能进行第一次字符串搜索。 现在我们继续进入INFO LINE。 使用正则表达式包,我们编译一个捕获TITLE_LINE的正则表达式。 一旦找到它,我们从迭代器得到下一行,再次检查字符串是否包含ENABLED或DISABLE; 并相应地打印。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.