Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use tee to send all output to stdout and grepped output to file?

I can use tee to send output to both stdout and file like this:

./process.sh | tee output.log

How do i send complete output to stdout and grepped output to file?

This one won't work, because tee expects a second file argument:

./process.sh | tee | grep foo > output.log
like image 638
Sathish Avatar asked Feb 08 '13 18:02

Sathish


2 Answers

You could try:

./process.sh | { tee /dev/tty | grep foo > output.log; }

this doesn't send output to stdout, but to the tty. Perhaps that is good enough.

or you could do:

./process.sh | awk '/foo/{ print > "output.log"} 1'

which prints all the output of process.sh to stdout, and lines that match foo are written to the file.

Also, you can do:

mkfifo fifo
./process.sh | { cat fifo & tee fifo | grep foo > output.log; }
rm fifo

That can be done more cleanly with a /proc filesystem:

./process.sh | { tee /proc/self/fd/6 | grep foo > output.lot; } 6>&1
like image 132
William Pursell Avatar answered Sep 22 '22 17:09

William Pursell


In addition to another answer which redirects output to /dev/tty, if you use a shell (and an OS) supporting bash-style process substitution, you can do this:

./process.sh | tee >(grep foo > output.log)
like image 41
Anton Kovalenko Avatar answered Sep 21 '22 17:09

Anton Kovalenko