Awk 日志文件包含带有消息戳的日志语句,如下所示

Awk 日志文件包含带有消息戳的日志语句,如下所示,awk,logfile,Awk,Logfile,日志文件包含带有消息戳的日志语句,如下所示。 该实用程序列出了超过指定时间的所有操作。从命令行以秒为单位指定时间 输入: #<process id> <date time> <log level> <file name> <line number> <actual message> 1098 2007-02-28 15:23:09 WARN db_util.c 5928 Config file not found, usi

日志文件包含带有消息戳的日志语句,如下所示。 该实用程序列出了超过指定时间的所有操作。从命令行以秒为单位指定时间

输入:

#<process id> <date time> <log level> <file name> <line number> <actual message>

1098 2007-02-28 15:23:09 WARN db_util.c 5928 Config file not found, using default values
1098 2007-02-28 15:23:09 INFO db_util.c 5908 Connecting to database
1098 2007-02-28 15:23:17 INFO db_util.c 5908 Connected to database
1098 2007-02-28 15:23:17 ERROR log_test.c 198 Unable to setup our satellite launch system
1098 2007-02-28 15:23:18 INFO log_test.c 198 Reconnecting to launch the satellite
1098 2007-02-28 15:23:21 INFO log_test.c 198 Reconnected. Initialize to launch the satellite
我发现此代码适用于上述问题声明:

awk -v threshold=$1 '{texttime=mktime(gensub(/-|:/," ", "g", $2" "$3))} NR>1 && texttime-prevtexttime>threshold{print prevrecord} {prevtexttime=texttime; prevrecord=$0}' $2

有没有更优雅的方法将当前记录的日期和时间转换为unix时间戳?除了这个
(mktime(gensub(/-):/,“,”g“,$2”“$3))
和awk?不是真的(只需将
/-|::/
更改为
/[-:]/
)。我相信perl有
strtime()
,这将为等效代码节省一个函数调用,但我认为仅仅为此更改工具/语言是不值得的。

感谢Ed Morton的回复。不使用awk的另一种方法是什么?请参阅以了解下一步的操作。
awk -v threshold=$1 '{texttime=mktime(gensub(/-|:/," ", "g", $2" "$3))} NR>1 && texttime-prevtexttime>threshold{print prevrecord} {prevtexttime=texttime; prevrecord=$0}' $2