How to measure size of piped data?

You can use wc for this:

grep pattern file.txt | wc -c

will count the number of bytes in the output. You can post-process that to convert large values to “human-readable” format.

You can also use pv to get this information inside a pipe:

grep pattern file.txt | pv -b > output.txt

(this displays the number of bytes processed, in human-readable format).


You can use the pipeviewer tool pv with the total byte count flag -b:

$ dd if=/dev/zero bs=3 count=4211 2>/dev/null | pv -b >/dev/null
12.3KiB

$ grep pattern file.txt | pv -b >/dev/null

The Pipe Viewer utility was designed for this purpose. If it's not flexible enough for your purposes, then you can implement your own FIFO data transfer measuring code with the pipeline manipulation library (libpipeline) function calls such as pipeline_pump() and pipeline_peek_size().

$ whatis pv
pv (1)               - monitor the progress of data through a pipe
$ pv -Wi 0.002 -cf /etc/hosts | wc -l
 367 B 0:00:00 [2.71MiB/s] 
[============================================================================>] 
100%
10
$

Tags:

Pipe