Ubuntu – Output not redirecting to file

greppingredirect

I'm experiencing weirdness (or rather showing Linux ignorance).

I want to learn the probability distribution of ping requests, so I thought I'd ping some random site for a while, grep the output to save the ms only, and then redirect to a file. I do this.

ping www.doggiedooley.com | grep -o "[[:digit:]]*.[[:digit:]]* ms" > ping.txt

I had high hopes for this command, but even though the file is created, it contains nothing, even after a while. If I remove the redirection, output is shown. I can even redirect the output successfully to another terminal.

ping www.doggiedooley.com | grep -o "[[:digit:]]*.[[:digit:]]* ms" > /dev/pts/1

Why is the first command not working properly?

Best Answer

Looks like a stdout buffering issue. You can turn it off before redirecting with:

stdbuf -o0

So use:

 ping www.doggiedooley.com | stdbuf -o0 grep -o "[[:digit:]]*.[[:digit:]]* ms" > ping.txt

Or use -c option of ping to limit sending packets.

Related Question