Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to redirect output in bash to different places with different filters?

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...

like image 718
Palace Chan Avatar asked Aug 30 '12 20:08

Palace Chan


1 Answers

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]
like image 97
Grisha Levit Avatar answered Oct 02 '22 02:10

Grisha Levit