Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 at 12:30
    
@Khaled, I need to watch how many new lines were appended –  fl00r Oct 22 '12 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 '12 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 '12 at 18:02
    
@cjc it works, thanks! :) you should at it as ab answer –  fl00r Oct 22 '12 at 18:13

3 Answers 3

up vote 5 down vote accepted

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
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
    
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 '12 at 17:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.