4

I am looking for a tool which does something similar to

tail -f /var/log/some.log | grep EVENT1 |rate

which keeps displaying the rate of the event.

sheki
  • 8,991
  • 13
  • 50
  • 69

2 Answers2

6
tail -f /var/log/some.log | grep --line-buffered EVENT1 | pv -l > /dev/null

pv is a pipe monitor, which outputs statistics on stderr; the -l will measure lines instead of bytes.

You'll need to use --line-buffered on your grep call, so that it doesn't buffer larger blocks, or for a general case you can use stdbuf to adjust your buffering.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • I like the way you think :) You're a bit slower than me, but I didn't know about `-l` to pv, so +1 – sehe May 19 '14 at 23:04
  • @sehe Thanks! Yeah. I think we spent the same 90 seconds testing our respective `pv`s in a shell before posting pretty similar things. Likewise +1 to you for the example output. – Jeff Bowman May 19 '14 at 23:08
1

Assuming you mean events/second:

while read line; do echo -n "$(grep -c EVENT1 <<<"$line")"; done | pv >/dev/null

This uses pv, an extremely simple utility. sudo apt-get install pv on most distrubutions (or yum -i etc.)

Output, when the input is e.g. (while true; do echo yes; sleep .1; done):

218B 0:00:22 [9,88B/s] [    <=>       ]

The technique used is to replace each matching line with a single byte in the output (namely '1') and just measure output bandwith in bytes-per-second :)

sehe
  • 374,641
  • 47
  • 450
  • 633