linux - 只显示多个文件的 grep 找到的输出

标签 linux bash awk grep xargs

我正在使用 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/

相关文章:

bash - 如果文件 ~/.bashrc 不存在,我如何在 Google Colab 上安装 Torch?

bash - 重命名大量文件的扩展 Bash

bash - 识别带引号空格的列

linux - Awk 和 Grep 的问题

linux - 如何用netstat分别打印IP和端口?

regex - 如何获取括号中指定的内容?

python - 根据字符串重命名文件

linux - 在 bash 中使用 find 命令查找多个不同类型的文件

linux - 如何获得所有者 :group name of a file/directory with perl?

linux - 在 Puppet 中获取机器的私有(private) ip