2016-02-25 43 views
1

'我創造健康檢查腳本爲檢查Oracle datagaurd狀態Python的打印前行,如果在當前行的條件滿足

的datagaurd狀態輸出會像

<Database_Name 1> - Primary database 
    Warning: ORA-16817: unsynchronized fast-start failover configuration 
<Database_Name 2> - (*) Physical standby database 
    Warning: ORA-16817: unsynchronized fast-start failover configuration 

,或者也一定會喜歡

<Database_Name 1> - Primary database 
    Warning: ORA-16817: unsynchronized fast-start failover configuration 
<Database_Name 2> - (*) Physical standby database 
    Warning: ORA-16817: unsynchronized fast-start failover configuration 

,所以我的要求是,當錯誤「ORA」發生時數據庫具有ORA-錯誤

方式打印前行

輸出很小我可以保留所有的行是緩衝區,但不行:行不是恆定的。那麼,有沒有方法來獲取行號ORA會在那裏等打印前行 這是我曾嘗試

#!/usr/bin/env python import sys, os, time, threading, subprocess,datetime,re command = 'su - emadba -c \'dgmgrl -silent sys/[email protected] "show configuration"\'' dgstatus = subprocess.Popen(command,stdout=subprocess.PIPE,shell=True) output = dgstatus.communicate()[0].strip().split("\n") prev = "" print prev for x in output: prev = x 
+0

'#在/ usr /斌/包膜蟒蛇 進口SYS,OS,時間,線程,子流程,日期時間,重新 命令= '蘇 - emadba -c \' DGMGRL -silent SYS /密碼@ emadb「秀配置「\'' dgstatus = subprocess.Popen(command,stdout = subprocess.PIPE,shell = True) output = dgstatus.communicate()[0] .strip()。split(」\ n「) prev = 「」 print prev for x in輸出: prev = x ' 這是我以前試過的 –

+0

請更新您的文章的代碼 - 評論並非用來發布的。 –

回答

1

當通過迭代行的代碼,你就不能保留以前的每次迭代都行嗎?

# Holds the previous line 
prev_line = None 
for line in open(<something>, 'r'): 
    if is_ora_line(line) and prev_line is not None: 
     do_something_with_database_line(prev_line) 
    # Remember now the current line as the previous line 
    prev_line = line 
+0

我用下面的代碼得到輸出,謝謝 ' command ='su - emadba -c \'dgmgrl -silent sys/password @ emadb「show configuration」\'' dgstatus = subprocess.Popen(command,stdout = subprocess.PIPE,shell = True) output = dgstatus.communicate()[0] .strip()。split(「\ n」) prev =無 對於輸出中的x: 如果re.search(「ORA」 ,x)或re.search(「db」,x)和prev不是無: print prev prev = x ' –

1

如果可以將錯誤作爲字符串獲取,則可以使用string.count("\n")來獲取換行符的計數。您也可以使用string.split("\n"),它返回字符串的一行數組。 array[-1]是數組中的最後一行,而array[-2]是倒數第一行。 !