13

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.

6
  • What are you trying to do? If you want to watch appended lines, it is enough to use tail -f /path/to/file alone. If you want to watch file lines, you can use watch wc -l /path/to/file.
    – Khaled
    Oct 22, 2012 at 12:30
  • @Khaled, I need to watch how many new lines were appended
    – fl00r
    Oct 22, 2012 at 12:31
  • I use tail with regex for a number of files, and I want to see how many lines are appended to all of this files through a time
    – fl00r
    Oct 22, 2012 at 12:35
  • 1
    Hmpfh. tail -f /var/log/my_process/*.log |grep error > /tmp/error.lines & ; watch wc /tmp/error.lines ? Then do math.
    – cjc
    Oct 22, 2012 at 18:02
  • @cjc it works, thanks! :) you should at it as ab answer
    – fl00r
    Oct 22, 2012 at 18:13

4 Answers 4

31

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
1
  • 1
    pv is a command that is not installed by default. Try doing sudo apt install pv before if it fails for you
    – fernandezr
    Mar 28, 2023 at 8:16
3

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.

3

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
-1

You could try something like this:

tail -f /var/log/my_process/*.log | perl -pe '$_ = "$. $_"'

It prints the line number in front of each line.

1
  • 1) That's not what the OP is looking for and 2) nl will do this, not requiring the call out to perl.
    – EEAA
    Oct 22, 2012 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .