If I have a process a.out
I can do ./a.out | grep foo
to see stdout of a.out filtered by foo. I can also say ./a.out 2>&1 | grep foo
to see both err and out filtered by foo. With the tee
command I can send stdout to both the terminal and possibly a file output. But is there a way to filter those separately? as in:
./a.out | tee grep foo file.txt
but such that what goes to file.txt
is filtered to match foo but not what I see on the screen...or even better what I see on the screen gets filtered by baz instead of foo? If there isn't a way to do so in bash already I would write my own "tee" but I would imagine there is some way...
Very easy, just use process substitution for your file handles:
./a.out | tee >(grep foo > out.txt) | grep baz
Note also that tee
can take as many arguments as you like, so you can do things like:
./a.out | tee >(grep foo > foo.txt) >(grep bar > bar.txt) [etc]
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