禁止包含特定字符串的命令输出行

spi*_*ace 1 command-line text-processing

当我在Linux终端中运行任意命令时,有没有办法抑制包含特定句子的输出消息?

我试过

./mycommand | grep -v "I dont want to see this"
Run Code Online (Sandbox Code Playgroud)

但消息仍然存在。

cmk*_*mks 6

也许不需要的部分是输出到 stderr 的一部分,而不是输出到 stdout 的部分。

尝试:

./mycommand 2>&1 | grep -v "I dont want to see this"
Run Code Online (Sandbox Code Playgroud)

您可以将 stderr 和 stdout 通过管道传输到不同的目标。所以你可能会看到输出来自哪里:

./mycommand >>(grep -v "我不想看到这个" > stdout.log) 2>>(grep -v "我不想看到这个" > stderr.log)

  • 另请注意,Bash 有“|&”,它是“2>&1 |”的简写。 (2认同)