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....
Use the tail command to get the last 2-3 records as shown below. In the above log the date format is 20/Aug/2021:07:23:07 that is DD/MMM/YYYY:HH:MM:SS. Now here is the awk command to extract data for the last 2 minutes. In the above command, %d/%b/%Y:%H:%M:%S is the format specifier of your date column.
For searching files, the command syntax you use is grep [options] [pattern] [file] , where “pattern” is what you want to search for. For example, to search for the word “error” in the log file, you would enter grep 'error' junglediskserver. log , and all lines that contain”error” will output to the screen.
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....
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....
edit
"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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With