views:

107

answers:

2

Duplicate http://stackoverflow.com/questions/570984/how-can-i-gzip-standard-in-to-a-file-and-also-print-standard-in-to-standard-out

I'm trying to count the lines from a command and I'd also like to see the lines as they go by. My initial thought was to use the tee command:

complicated_command | tee - | wc -l

But that simply doubles the line count using GNU tee or copies output to a file named - on Solaris.

A: 

The solution is to tee to the console directly as opposed to STDOUT:

tty=`tty`
complicated_command | tee $tty | wc -l
Jon Ericson
+3  A: 
complicated_command | tee /dev/tty | wc -l

But keep in mind that if you put it in a script and redirect the output, it won't do what you expect.

Paul Tomblin
Thanks. I wonder why I didn't know that /dev/tty points to my terminal name. Glad I asked since that reduces my code a bit.
Jon Ericson
Yeah, /dev/tty is an alias for your current tty. It's very useful like that.
Paul Tomblin