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
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With