Bohemian
Bohemian

Reputation: 425258

Regex for timestamps in the first 5 minutes after the hour

I have a log file for a process that logs every 5 minutes, but not at an exact minutes/seconds and I want to find the first log message of each hour. A sample line looks like:

2013-01-01 09:03:55 foo bar some log message foo bar

What would be the regex to use with grep to extract those first-of-the-hour lines?

Upvotes: 0

Views: 1158

Answers (2)

Anders Johansson
Anders Johansson

Reputation: 4006

You cannot use grep to get only the single first line for each hour since it doesn't keep state between matches. Something like this would get all lines in e.g. the first five minutes every hour:

grep '\d\d:0[0-5]:\d\d' logfile

You can get just the first entry from the first five minutes every hour with awk:

awk -F'[ :]' 'BEGIN {hh=-1} $2!=hh && $3>=0 && $3<5 {hh=$2; print}' test

Upvotes: 1

Lars Kotthoff
Lars Kotthoff

Reputation: 109262

Something like

grep "[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} [0-9]\{2\}:0[0-5]" logfile

Upvotes: 1

Related Questions