如何在发送到文件之前"处理"tee中的文本

lac*_*991 2 bash shell debian

有没有办法在发送到文件之前处理tee中的文本?

例如,如果程序输出以下行:

stack 11
stack 22
stack 33
serverfault
serverfault
stack 44
Run Code Online (Sandbox Code Playgroud)

我怎么能只将包含"stack"的行发送到文件,同时仍然通过stdout显示程序的原始输出?

我不确定是否可以使用发球台做到这一点,所以我愿意接受任何其他建议.

谢谢

Joh*_*024 6

要在终端上显示myprogram的完整输出,同时将过滤后的输出保存到文件中:

myprogram | tee /dev/tty | grep stack >out
Run Code Online (Sandbox Code Playgroud)

如果您不想将完整输出发送到您的终端,而是想用其他东西来处理它,比如说someotherprogram,那么我们可以使用bash的进程替换:

myprogram | tee >(grep stack >out) | someotherprogram
Run Code Online (Sandbox Code Playgroud)

someotherprogram接收myprogram其stdin上的完整输出,同时仅保存过滤后的输出out.