bash:使用 cat/grep 长参数列表

bash: use cat/grep with long argument list

我在bash中有以下结构:

cat myfiles_* | grep "mystring" > summary.txt

如果文件太多,命令将失败并显示 "argument list too long"。

通常,人们会使用 xargs 来避免 "argument list too long" 错误,但在这种情况下,必须连接文件,所以这不是一种选择。

是否有另一种方法可以从(太多)文件列表中过滤 "mystring"?

您不应该将 cat 与读取数据本身的程序一起使用,请尝试:

grep "mystring" myfiles_* > summary.txt

你也可以试试awk

awk '/mystring/' myfiles_* > summary.txt

试试这个来避免 bash 的 globbing:

引起的 "argument list too long" 错误
find . -name 'myfiles_*' -type f -exec grep "mystring" {} \; > summary.txt

您还可以为 find 提供 -maxdepth 1 选项,以防您不想遍历子目录。这符合您的 cat myfiles_* 行为。