如何过滤某个时间范围内的日志

aLe*_*LeX 6 grep sed awk text-processing

这是我的日志格式(为了演示而简化)

2018-04-12 14:43:00.000 ERROR hello
2018-04-12 14:44:01.000 ERROR world
2018-04-12 14:44:03.000 INFO this is a multi-line log
NOTICE THIS LINE, this line is also part of the log
2018-04-12 14:46:00.000 INFO foo
Run Code Online (Sandbox Code Playgroud)

那么如何过滤日志[2018-04-12 14:44:00.000, 2018-04-12 14:45:00.000)以产生以下输出呢?

2018-04-12 14:44:01.000 ERROR world
2018-04-12 14:44:03.000 INFO this is a multi-line log
NOTICE THIS LINE, this line is also part of the log
Run Code Online (Sandbox Code Playgroud)

Sté*_*las 5

awk

awk -v 'start=2018-04-12 14:44:00.000' -v end='2018-04-12 14:45:00.000' '
   /^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} / {
     inrange = $0 >= start && $0 <= end
   }
   inrange' < your-file
Run Code Online (Sandbox Code Playgroud)

它不适用于mawk不支持 POSIX 字符类和间隔正则表达式运算符的情况。