Python打印与2个时间戳之间的模式匹配的所有日志行

2024-09-30 12:26:36 发布

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

打印在开始时间和结束时间之间匹配特定图案的线条的工作。在

日志行示例:

Fri Mar 01 HH:MM:SS YYYY:thresholdcrossed

我正在寻找一个等效的如下:

cat /path/file.log | egrep -v "exec|param" | grep thresholdcrossed | sed -n /$start/,/$stop/ >output.file

start和stop将是用户根据搜索的开始和停止时间生成的变量。在

这将给我一个使用正则表达式的行的匹配,但我无法得到所有两个时间戳之间匹配的行。在

^{pr2}$

Tags: 示例hh时间startssmarcat线条
1条回答
网友
1楼 · 发布于 2024-09-30 12:26:36
# user defined time range in milliseconds since epoch and
start_ms = 
end_ms = 
# user defined pattern
pattern = "tstat-threshcrosso"

# loop through whole file
for line in hand:
    # strip the line
    line = line.rstrip()

    # parse the line to get time_stamp_ms (ms since epoch) and log_message
    time_stamp_ms = 
    log_message =

    # break if we went beyond end time
    if time_stamp_ms > end:
        break

    # check if the time stamp is within user defined period
    if time_stamp_ms < start_ms:
        continue

    # check for string pattern match
    if pattern in log_message:
        print line

您可以通过执行二进制搜索来优化这一点,以便在开始时间之后找到第一行。在

相关问题 更多 >

    热门问题