我正在使用 grep 在文件中查找字符串。 grep 可能只会找到一个与文件名匹配的文件,可能是两个,但我只需要它来放置字符串(如果找到),并且只是字符串。
grep -oh 'Closing finished' /opt/cpu/hold/closing15{14..23}
目前我得到的输出如下:
grep: /opt/cpu/hold/closing1514: No such file or directory
Closing finished
Closing finished
grep: /opt/cpu/hold/closing1517: No such file or directory
grep: /opt/cpu/hold/closing1518: No such file or directory
我在 bash 脚本的函数中使用它,在直到循环中匹配“关闭完成”,然后再继续。所以我希望输出只是“关闭完成”(如果找到),以便它符合我的直到条件。
最佳答案
如果您想要抑制有关不存在文件的错误,只需使用 -s
选项:
-s, --no-messages
Suppress error messages about nonexistent or unreadable files.
即您的示例命令应如下所示:
grep -soh 'Closing finished' /opt/cpu/hold/closing15{14..23}
或者,您也可以通过将错误输出重定向到/dev/null 来完全抑制错误输出,如下所示:
grep -oh 'Closing finished' /opt/cpu/hold/closing15{14..23} 2>/dev/null
关于linux - 只显示多个文件的 grep 找到的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59758872/