Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use pipe with du to compute "size" of stdin

Tags:

linux

stdin

du

I have a program in which I load text from a file and then filter it according to one of the fields. What I am interested in is the size of the data after this filtering step.

Ideally, I would be able to do something like: awk '$2>=10' <myfile | du -

I could just apply the filter and save the output somewhere, call du on it, and then delete the file, but the file is quite large, so writing to disk could take a while.

Recognizing that du stands for "disk usage", I suspect I am asking something that makes no sense, given how the program works. If there is another common utility that will do this, please suggest it!

like image 424
reo katoa Avatar asked Nov 16 '12 14:11

reo katoa


People also ask

How do you calculate data in pipe size?

There is no portable way to tell the amount of data coming from a pipe. The only thing you could do is to read and process data as it comes. Save this answer.

Does pipe use Stdin?

Simple piping uses | character to send the STDOUT from one application to the STDIN of the next application. One nice thing about piping in Linux is that each application that is executed is run in parallel, so each application is processing its STDIN and sending its STDOUT as soon as it is received.

What is the size of Stdin?

if stdin/stdout are connected to a terminal then default size = 1024; else size = 4096.


2 Answers

You can pipe it to wc -c to count the number of bytes that goes through the pipeline.

like image 61
Brian Campbell Avatar answered Sep 21 '22 11:09

Brian Campbell


du stands for "disk usage". Data in a pipe doesn't hit the disk, so there's no "du" to work with. use wc instead, which is "word count".

awk '$2>=10' < myfile | wc -c

The -c flag counts bytes.

like image 41
Marc B Avatar answered Sep 20 '22 11:09

Marc B