结合grep和and&or logi

2024-09-28 13:21:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将以下用python编写的代码转换为使用cat | 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

我必须删除打开文件的那一行,并用*** ***将下面的一行改为使用cat和grep的内容 例如:

^{pr2}$

但我不知道如何在同一个grep中组合或


Tags: nameinlogtimegreptmprintstr
1条回答
网友
1楼 · 发布于 2024-09-28 13:21:06

“或”可以通过一次重映射多个模式来模拟:

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

-E告诉egrep使用扩展regex。在

据我所知,grep中没有“AND”操作符,但是可以通过greping来模拟正向和反向的模式。在

^{pr2}$

使用-v表示“不”。在

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

这将找到所有有“thing1”的东西,然后只抓取没有“thing2”的东西。在

希望这有帮助。在

相关问题 更多 >

    热门问题