user99999
user99999

Reputation: 2022

How to grep logs from specific date

I have the following log format:

[23:34:43.669] Notice [2] Some notice on line 21

Line by line, and I want to use grep to find only those lines that are from 15:00 to 21:00.

How can I do that?

I've been trying something like grep '\[21:.*?\].*?' file.txt

But it returns empty output. How do I do this?

Upvotes: 0

Views: 100

Answers (2)

Izak Marais
Izak Marais

Reputation: 131

For me this works if I remove the ?:

grep '\[21:.*\].*' file.txt

I think your mistake is combining * and ?, which would make the ? apply to the *. You should rather use one only. For reference about ?in grep regexes, see this post.

Upvotes: 0

arco444
arco444

Reputation: 22821

It's much easier to do 15:00 to 20:59 if that's not a problem for you.

You can use the following grep pattern:

grep '\[\(1[5-9]\|20\).*\?\].*' file.txt

Upvotes: 1

Related Questions