2016-04-25 58 views
0

我想轉換下面的代碼是用python寫的使用cat | grep而不是打開文件。grep與和&或邏輯

原始代碼:

LOG_NAME="/raft/log/{}{}{}_{}_Rx_flow.log".format(NTIME.tm_year,str(NTIME.tm_mon).zfill(2),str(NTIME.tm_mday).zfill(2),str(NTIME.tm_hour).zfill(2)) 
print time.strftime("%Y/%m/%d %H:%M:%S") + " Log file name, RX restart, is: " + LOG_NAME 
print time.strftime("%Y/%m/%d %H:%M:%S") + " ERRTIMEMIN value: " + ERRTIMEMIN + " RXRESTART message value: " + RXRESTART 

LINK_LOG_FILE = open(LOG_NAME, "r") 
ISRXRESTART=0 
for REPline in LINK_LOG_FILE: 
    ***if RXRESTART in REPline and (ERRTIMEMIN in REPline or ERRTIMEMIN1 in REPline) and ISRXRESTART==0:*** 
    #Link restarted - correct behaviour. 
    print time.strftime("%Y/%m/%d %H:%M:%S") + " RX restarted - This is correct behaviour" 
    ISRXRESTART=1 

我不得不刪除這將打開文件中的行,並更改與*** ***以下行的東西貓和grep 例如:

os.popen("sshpass -p ssh [email protected]"+self.ipaddr+" cat "+LOG_NAME+" | egrep `"+device_start+" "+ERRTIMEMIN+`").read().strip() 

但我不知道如何組合或與&和在相同的grep

+0

@fedorquiI在問題的最後部分確切地說我想要什麼 – Sarit8

+1

更好地包括[mcve]。 – fedorqui

回答

0

「或」可以通過模擬simp ly一次刷新多個模式:

egrep -E 'thing1|thing2' <file.txt 

-E告訴egrep使用擴展正則表達式。

據我所知,在grep中沒有「AND」運算符,但是也可以通過以向前和向後順序對模式進行grep來模擬。

egrep -E 'thing1.*thing2|thing2.*thing1' <file.txt 

對於「NOT」使用-v

egrep -E 'thing1' <file.txt | egrep -v 'thing2' 

這將找到一切與「thing1」,然後只抓住沒有「thing2」的東西。

希望這有助於。