grep日志文件大于时间戳 [英] Grep log file greater than time stamp

查看:463
本文介绍了grep日志文件大于时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个unix日志文件(application.log),它有起始日志和时间戳记。我需要在这个日志文件中搜索发送模式的时间大于时间2014-03-20 14:05:54。

I have a unix log file(application.log), that has logs with timestamp at the starting. I need to search for a pattern "sent" in this log file greater than time 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....


推荐答案

我为测试数据添加了2条记录,以确保它真的有效:

I added 2 more records to the test data to ensure this is really working:

2014-03-19 14:05:53,999 [NfxAgent....
2014-03-20 14:05:53,164 [NfxAgent....

但我不认为你可以使用 grep 来达到这个目的。这是一个awk解决方案:

But I don't think you can use grep for this. Here is an awk solution:

$ 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:140018?

"What if we need to specify the end time in the same format like 2014-03-21 10:04:14,018?"

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

And I've added 3 lines of test data to confirm the 2nd case:

2014-03-21 10:04:14,017 [NfxAgent....
2014-03-21 10:04:14,018 [NfxAgent....
2014-03-22 10:04:14,999 [NfxAgent....

结果显示您指定范围内的一条新记录。

Result shows one new record in the range you've specified.

 x000294@nardhl011 1220>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....

IHTH

这篇关于grep日志文件大于时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆