Unix Grep日志文件大于时间戳

Unix Grep日志文件大于时间戳,unix,sed,awk,grep,Unix,Sed,Awk,Grep,我有一个unix日志文件(application.log),其中的日志在开始时带有时间戳。我需要在此日志文件中搜索大于时间2014-03-20 14:05:54的模式“已发送” 2014-03-20 14:05:54,038 [NfxAgent.... 2014-03-20 14:05:54,164 [NfxAgent.... 2014-03-20 14:05:54,298 [NfxAgent.... 2014-03-20 14:05:54,414 [NfxAgent.... 2014-03-2

我有一个unix日志文件(application.log),其中的日志在开始时带有时间戳。我需要在此日志文件中搜索大于时间2014-03-20 14:05:54的模式“已发送”

2014-03-20 14:05:54,038 [NfxAgent....
2014-03-20 14:05:54,164 [NfxAgent....
2014-03-20 14:05:54,298 [NfxAgent....
2014-03-20 14:05:54,414 [NfxAgent....
2014-03-20 14:05:54,787 [NfxAgent....
尝试:

注意:这假设日志文件中至少有一行以
2014-03-20 14:05:54
开头,并且可以包含第二个时间段的匹配项

如果不能保证存在这样一条线,@Sheller的
awk
方法更优越;总而言之:

awk '$0 > "2014-03-20 14:05:54" && $0 ~ "sent"' file

我在测试数据中添加了2条记录,以确保它真正起作用:

2014-03-19 14:05:53,999 [NfxAgent....
2014-03-20 14:05:53,164 [NfxAgent....
但是我认为你不能用grep来做这个。以下是一个awk解决方案:

$ grep sent  grepTest_20140321.txt|  awk '$0 > "2014-03-20 14:05:54"' 
2014-03-20 14:05:54,038 [NfxAgent....
2014-03-20 14:05:54,164 [NfxAgent....
2014-03-20 14:05:54,298 [NfxAgent....
2014-03-20 14:05:54,414 [NfxAgent....
2014-03-20 14:05:54,787 [NfxAgent....
编辑


“如果我们需要以类似2014-03-21 10:04:14018的格式指定结束时间,该怎么办?”

我添加了3行测试数据来确认第二种情况:

2014-03-21 10:04:14,017 [NfxAgent....
2014-03-21 10:04:14,018 [NfxAgent....
2014-03-22 10:04:14,999 [NfxAgent....
结果显示指定范围内的一条新记录

 awk '$0 ~ "sent" && $0 > "2014-03-20 14:05:54" && $0 < "2014-03-21 10:04:14,018"'    grepTest_20140321.txt
2014-03-20 14:05:54,038 [NfxAgent....
2014-03-20 14:05:54,164 [NfxAgent....
2014-03-20 14:05:54,298 [NfxAgent....
2014-03-20 14:05:54,414 [NfxAgent....
2014-03-20 14:05:54,787 [NfxAgent....
2014-03-21 10:04:14,017 [NfxAgent....
awk'$0~“已发送”和&$0>“2014-03-20 14:05:54”和&$0<“2014-03-21 10:04:14018”grepTest_20140321.txt
2014-03-20 14:05:54038[NfxAgent。。。。
2014-03-20 14:05:54164[NfxAgent。。。。
2014-03-20 14:05:54298[NfxAgent。。。。
2014-03-20 14:05:54414[NfxAgent。。。。
2014-03-20 14:05:54787[NfxAgent。。。。
2014-03-21 10:04:14017[NfxAgent。。。。

IHTH

如果我们需要为
awk'$0>“2014-03-20 14:05:54”
指定与2014-03-21 10:04:14018?+1相同格式的结束时间,但您缺少对模式“sent”的额外搜索,该怎么办。请参阅编辑以获取解决方案。确切地说,您可能会将018更改为019或用户
@mklement0:我已更新了您案例的原始答案,并为您的眼睛添加了+1,但没有找到。但是……它不在示例数据中。@Shelleter:谢谢更新;我还建议集成搜索
“已发送”
直接输入
awk
程序,这样就不需要单独的
grep
命令(如我的回答中所示)。可能会重复
 awk '$0 ~ "sent" && $0 > "2014-03-20 14:05:54" && $0 < "2014-03-21 10:04:14,018"'    grepTest_20140321.txt
2014-03-20 14:05:54,038 [NfxAgent....
2014-03-20 14:05:54,164 [NfxAgent....
2014-03-20 14:05:54,298 [NfxAgent....
2014-03-20 14:05:54,414 [NfxAgent....
2014-03-20 14:05:54,787 [NfxAgent....
2014-03-21 10:04:14,017 [NfxAgent....