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

时间:2017-06-12 19:37:17

标签: python regex

在开始时间和结束时间之间打印与特定模式匹配的行。

示例日志行:

  

Fri Mar 01 HH:MM:SS YYYY:thresholdcrossed

我正在寻找以下内容:

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

启动和停止将是用户生成的变量,基于他们搜索的开始和停止时间。

这会为我提供使用正则表达式的单行匹配,但我无法获得在2个时间戳之间匹配的所有行。

for line in hand:
    line = line.rstrip()
    if re.search("Mar 10 21.+:.+tstat-threshcrosso", line)  :
        print line

1 个答案:

答案 0 :(得分:0)

# 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

您可以通过二进制搜索来优化此操作,以查找开始时间之后的第一行。