[英]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.