将文件列表传递给 grep [英] Pass a list of files to grep

查看:24
本文介绍了将文件列表传递给 grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件中有一个文件列表.这个列表很大,而且文件名是非标准的:这意味着,有些有空格、非 ASCII 字符、引号、单引号......

I have a list of files in a file. The list is huge, and the filenames are non-standard: that means, some have spaces, non-ascii characters, quotes, single quotes ...

因此,将 巨大 文件列表作为参数传递给 grep 不是一种选择:

So, passing that huge list of files to grep as arguments is not an option:

  • 因为我不确定我会不会超过 linux 中允许的参数长度.不过,我想我可以通过使用 xargs 对参数进行分区来解决这个问题.
  • 因为转义这些字符很复杂.如果我想用双引号将文件名括起来,而该文件名恰好有一个双引号,我就有麻烦了.所以我需要转义一些字符.整个事情看起来很复杂,我不想走那条路.
  • because I am not sure that I will not exceed the length of the arguments allowed in linux. I guess I can solve this by partitioning arguments with xargs, though.
  • because escaping those characters is complicated. If I want to enclose the filename in double quotes and that filename happens to have a double quote, I am in trouble. So I would need to escape some characters. The whole thing looks very complicated, and I do not want to walk that path.

必须有一个更简单的方法:我如何告诉 grep 使用我的文件列表作为 grep 的文件?我假设由于 shell 不会处理文件列表,因此转义和参数长度不再是问题.问题是grep是否支持这种操作方式,我在文档中已经找不到了.

There must be a simpler way: how can I tell grep to use my list of files as files to grep from? I assume that since the list of files would not be processed by the shell, escaping and argument length is not an issue anymore. The question is whether grep supports this operation mode, which I have been unable to find in the documentation.

推荐答案

据我所知,GNU grep 不支持这个.您有几个选择.

GNU grep does not support this as far as I know. You have a few options.

使用 bash 循环解析文件列表

这是@fedorqui 提供的解决方案

This is the solution provided by @fedorqui

while read file; do 
    grep "$PATTERN" "$file" 
done < file_with_list_of_files

使用 xargs 一次将多个文件传递给 grep

这里我告诉 xargs 将 10 个文件名传递给 grep

Here I've told xargs to pass 10 filenames to grep

PATTERN='^$' # searching for blank lines
xargs -n 10 -d '
' grep "$PATTERN" < file_with_list_of_files

使用xargs一次将多个文件传递给grep,处理文件名中的换行

同上,但使用空终止行

PATTERN='^$' # searching for blank lines
tr '
' '' file_with_list_of_files
xargs -n 10 -0 grep "$PATTERN" < file_with_list_of_files

正确处理空格Edit2: 删除产生乱码的示例,添加处理换行符的示例

deal with whitespace correctly Remove example that produces garbled output, add example that deals with newlines

这篇关于将文件列表传递给 grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆