How to watch for count of new lines in tail

I want to do something like this:

watch tail -f | wc -l
#=> 43
#=> 56
#=> 61
#=> 44
#=> ...

It counts new lines of tail each second

/ Linux, CentOs

To be more clear. I have got something like this:

tail -f /var/log/my_process/*.log | grep error

I am reading some error messages. And now I want to count them. How many ~ errors I have got in a second. So one line in a log is one error in a proccess.


I've recently discovered pv, and it's really cool, you could do something like

tail -f logfile | pv -i2 -ltr > /dev/null

  • -i2 = count every 2 seconds
  • -l = count lines
  • -t = print time
  • -r = show rate

Here's a quick and dirty method. You basically want to break the tail and the watch wc into separate parts, and do something like:

tail -f /var/log/my_process/*.log |grep error > /tmp/error.lines &
watch wc /tmp/error.lines

at which point, you can do math to get an errors/sec number. But, if you're just doing this for an one-off examination of your error rate, quick-and-dirty might be good enough.


In case pv is not available it can be done with perl:

Every one second:

tail -f  recycleBack*out  | perl -e 'while (<>) {$l++;if (time > $e) {$e=time;$i++;print "$i=> $l\n";$l=0}}'

Every 10 seconds

tail -f  recycleBack*out  | perl -e 'while (<>) {$l++;if (time > $e+10) {$e=time;$i++;print "$i=> $l\n";$l=0}}'

Sample output:

1=> 1
2=> 1523
3=> 1339
4=> 1508
5=> 1785
6=> 1587
7=> 1770
8=> 1432
9=> 1339
10=> 1555
11=> 1663
12=> 1693
13=> 1647