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

2

6

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
Laniferous answered 8/2, 2013 at 18:21 Comment(0)
B
5

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
Barely answered 8/2, 2013 at 18:25 Comment(0)
B
3

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)
Brazen answered 8/2, 2013 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.