How to grep a log file within a specific time period with string

I have a log file in this format:

2016-01-21 01:56:48,586 [http-nio-8320-exec-54] INFO config.web.login - Successful login. Username: XYZ, xxxxx

How do I grep this file and get only the lines where the time is between 1:50:00 and 1:56:00 and where the string config.web.login - Successful login. Username: XYZ is present? I'm trying to count the number of successful logins for a certain username.

2 Answers

You can do this with the help of awk and grep.

Try this code:

awk '/01:50:48/, /01:56:48/' <<FILENAME>> | grep 'Successful login. Username: XYZ,\s\+xxxxx'

Where /01:50:00/ is the start time and /01:56:00/ the stop time. Replace << FILENAME>> with the path to your log file.

I am assuming that you are interested in the 01:50:00 to 01:56:00 time frame on only the date 2016-01-21 that is shown in your example.

Here's a solution using only grep:

$ egrep '^2016-01-21 01:(56:00|5[0-5]:[0-5][0-9]).*[[:blank:]]config.web.login - Successful login. Username: XYZ,'

Edit: thank you to @toto for pointing out a bug in the previous revision of this answer.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like