Redirecting pv output to file

Viewed 5924

I'm using the pv utility to asses the speed of log entries being added in a log file, like this:

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

This outputs the addition speed and refreshes every 5 sec:

0:00:05 [  10/s]

Now, I would like to redirect the current value ("10") to a file and refresh it every 5s. Not append it, just overwrite the previous value in the file. Any idea on how to do the redirection to file? I'm pretty new in unix and haven't got it to work yet.

Thanks!

4 Answers

The other answers tell how to get pv to save to a file, but some are incomplete. pv terminates each logged line with a \r (carriage return or CR) and not a \n (newline). If you, for example, cat pv-output-file, it won't print each line separately to the screen, rather it'll print each line and then overwrite it almost instantly. more seems to behave somewhat better, but still not great, and less will give you the whole pv log on a single line rendered with a bunch of ^M's at what should be the end of each line.

To get it to render well in a text viewer, you'll still need to do something to translate the CR's to newlines. There are any number of ways to do this in unix including tr, sed, awk, and even one of the dos2unix variants (I think it's mac2unix or dos2unix -c mac).

If you have perl installed, the following one-liner will print each line of pv-output-file on a separate line.

perl -pe 's/\r/\n/g' pv-output-file

You can redirect the output of that into a text viewer like less or more to scroll through the lines. If you'd just want to 'fix' it in place, do perl -pi -e [...] as above.

Related