Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I "process" text from tee before it is sent to a file

Tags:

bash

shell

debian

Is there a way to process text from tee before it sent to a file?

For example, if a program outputted the following lines:

stack 11
stack 22
stack 33
serverfault
serverfault
stack 44

How could I send only the lines that contain "stack" to a file, while still displaying the original output from the program via stdout?

I'm not sure whether it would even be possible to do this using tee, so I am open to any other suggestions.

Thanks

like image 790
lacrosse1991 Avatar asked Dec 09 '25 05:12

lacrosse1991


1 Answers

To display the full output of myprogram on the terminal while saving filtered output to a file:

myprogram | tee /dev/tty | grep stack >out

If you didn't want to send the full output to your terminal but instead wanted to process it with something else, say someotherprogram, then we can use bash's process substitution:

myprogram | tee >(grep stack >out) | someotherprogram

someotherprogram receives the full output of myprogram on its stdin while only filtered output is saved in out.

like image 82
John1024 Avatar answered Dec 11 '25 17:12

John1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!