如何使用tee将所有输出发送到stdout并将grepped输出发送到文件?

Sat*_*ish 4 unix logging grep tee

我可以使用tee将输出发送到stdout和文件,如下所示:

./process.sh | tee output.log
Run Code Online (Sandbox Code Playgroud)

如何将完整输出发送到stdout和grepped输出到文件?

这个不起作用,因为tee期望第二个文件参数:

./process.sh | tee | grep foo > output.log
Run Code Online (Sandbox Code Playgroud)

Wil*_*ell 5

你可以尝试:

./process.sh | { tee /dev/tty | grep foo > output.log; }
Run Code Online (Sandbox Code Playgroud)

这不会将输出发送到stdout,而是发送到tty.也许这就足够了.

或者你可以这样做:

./process.sh | awk '/foo/{ print > "output.log"} 1'
Run Code Online (Sandbox Code Playgroud)

它打印process.sh到stdout的所有输出,匹配的行foo被写入文件.

此外,你可以这样做:

mkfifo fifo
./process.sh | { cat fifo & tee fifo | grep foo > output.log; }
rm fifo
Run Code Online (Sandbox Code Playgroud)

使用/proc文件系统可以更干净地完成:

./process.sh | { tee /proc/self/fd/6 | grep foo > output.lot; } 6>&1
Run Code Online (Sandbox Code Playgroud)