Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to do some simple computation of the number of lines per minute added to a log file.

I want also to store the count for each second.

What I need is the output of the following command as a list which will be updated every second:

watch -n1 'wc -l my.log'

How to output the 'update' of the 'watch' command as a list?

share|improve this question
add comment

3 Answers

You can use the -t switch to watch which causes it not to print header. However, that will still clear the screen so you might be better off with a simple shell loop:

while sleep 1; do
    wc -l my.log
done

One of the advantages is, that you can easily add other commands (e.g. date) and/or pipe the output through sed to reformat it. By the way, if you swap sleep 1 with wc in the loop, it will automatically terminate on errors.

share|improve this answer
    
However note that it will not do it exactly every second (with zsh or ksh93, you could adjust the sleeping time so as to account for the drift incurred by running the commands in the loop, though) –  Stephane Chazelas Nov 19 '12 at 11:51
1  
@StephaneChazelas neither will it with wait - just try watch -n 1 "sleep 5". –  peterph Nov 19 '12 at 12:26
    
Indeed (I've checked both procps and busybox implementations). I thought that was the only thing watch was useful for, but it doesn't give you even that so your solution is as good as a watch based one, but neither answers the "rate at which a log file grows" question with great accuracy. –  Stephane Chazelas Nov 19 '12 at 13:22
    
Well, if lines per minute are the ultimate goal, then sampling it once every 10 seconds is more than enough imho - so the overhead is not that terrible (unless the file grows really big, of course). And actually from within the loop, one can print timing information (even both before and after the command finishes if necessary), and then the accuracy can get better (orders of magnitude) even for bigger files. –  peterph Nov 19 '12 at 13:28
add comment

How about

tail -f file.log | pv -rl > /dev/null
share|improve this answer
add comment

An old question, but I just found a very easy answer:

watch -n1 'wc -l my.log | tee statistics.log'

This will execute your wc each second, add its output to the statistics.log file, and also show it on the screen.
So, you'll end up with a file filled with numbers, representing the successive number of lines of my.log.

share|improve this answer
add comment

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.