Grep log file larger than timestamp

I have a unix log file (application.log) that has timestamped logs on startup. I need to find the template "sent" in this log file more 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.... 
+7
unix grep awk sed
source share
2 answers

I added 2 more entries to the test data to make sure this really works:

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

But I do not think you can use grep for this. Here is the 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.... 

change

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

And I added 3 lines of test data to confirm the second 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.... 

The result shows one new record in the specified range.

 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

+7
source share

Try:

 sed -n '/^2014-03-20 14:05:54/,$ {/sent/p;}' file 

Note. It is assumed that there is at least 1 line in the log file that starts from 2014-03-20 14:05:54 , and that it is OK to include matches from this second in time.

If the existence of such a line is not guaranteed, the @shellter awk approach is superior; to combine:

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

All Articles