2

I have an application which emits logs in this format:

00:00:10,799 ERROR [stderr] (http-prfilrjb08/10.1.29.34:8180-9)     {}:return code:  500

I would need to monitor for new ERRORs in the log file, happened in the last hour. Looking at some tutorials I've come up with the following grep:

 grep "^$(date -d -1 hour +'%H:%M:%S')" /space/log/server.log  | grep 'ERROR'

However nothing is grepped! Can you help me to fix it ? Thanks!

Carla
  • 3,064
  • 8
  • 36
  • 65
  • I guess you need quotes after `-d` --> `date -d '-1 hour' +'%H:%M:%S'`. However, it would be good to have a proper [mcve]. – fedorqui Jun 27 '16 at 12:52

2 Answers2

5

You need quotes around the -1 hour and also you want to remove the seconds and minutes from the output (your current solution finds data only for the first second 1 hour ago):

grep "^$(date -d '-1 hour' +'%H')" /space/log/server.log  | grep 'ERROR'
Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
3
grep -E "^($(date -d '-1 hour' '+%H')|$(date '+%H')):[0-9]{2}:[0-9]{2}" /space/log/server.log | grep 'ERROR'

Let's take a look at the parts

grep -E tells grep to use extended regular expressions (so we don't need to escape all those brackets)

date -d '-1 hour' '+%H' prints the previous hour. Similarly date '+%H' prints the current hour. These need to be evaluated at runtime and captured in a capture group, that's why we have the (date|date) structure (you'll probably want some data not only from the previous hour, but the current running hour).

Next you need to specify that you are indeed looking at timestamps. We use : to delimit hours, minutes and seconds. A two-digit number group can be matched with the [0-9]{2} regexp (this is basically identical to [0-9][0-9] but shorter)

There you go.

Ps. I'd recommend sed.

Esa Lindqvist
  • 311
  • 2
  • 6