Redirecting pv output to file Redirecting pv output to file unix unix

Redirecting pv output to file


pv is writing its diagnostics to stderr, so you can start with:

tail -f -n 0 mylog.log | pv -lr -t -i 5 > /dev/null 2> output-file

This will merely append, so output-file will end up with all of the data with lines terminated by carriage returns without linefeeds. (I don't have access to pv at the moment, so this is all conjecture.) To get the data you want, try some simple post processing along the lines of:

tail -f mylog.log | pv -lr -t -i 5 2>&1 > /dev/null |   tr /\\r \ \\n | tr -d [] | while read stamp value;  do echo $value > output-file; done

The shell technique above allows you to direct stderr into the pipe for post-processing. Using tail like you're doing is a little odd, and pv is overkill for this. If you have such a long delay (5 seconds is a long time), it's probably acceptable to just use wc on each iteration and do subtraction:

echo 0 > output-filewhile sleep 5; do    expr $(wc -l < mylog.log) - $(cat output-file) > output-filedone

Note that this prints the difference over 5 seconds rather than a rate per second. expr does not handle non-integer arithmetic very well, so dividing by 5 is problematic. You could use bc or dc to get more precision, but you're probably better off not even doing subtraction and just writing the total number of lines to the file every few seconds.


As I can't add a comment, I want to follow up on this one

Use the -f flag on pv to force its output to a file.

-f, --force Force output. Normally, pv will not output any visual display if standard error is not a terminal. This option forces it to do so.

e.g. tail -f -n 0 mylog.log | pv -lrf -t -i 5 > /dev/null 2> output-file


If the byte count of the file is OK instead of the line count, you can do this:

while true; do  ls -s --block-size=1 mylog.log > output-file  sleep 5end

This is much more efficient than using PV. The "pv" command is burning a lot of CPU by copying all the bytes in your log.