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

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

问题描述

我有一个文件列表中的文件。这个列表非常庞大,文件名是非标准的:这意味着,一些文件名包含空格,非ascii字符,引号,单引号......

列出的文件以grep作为参数不是一个选项:




  • 因为我不确定我不会超过linux允许的参数长度。不过我想我可以通过用 xargs 来分割参数来解决这个问题,因为转义这些字符很复杂。

  • 如果我想用双引号括起文件名,并且文件名碰巧有双引号,我就麻烦了。所以我需要逃避一些人物。整个事情看起来非常复杂,我不想走这条路。



必须有一个更简单的方法:我怎么能告诉grep使用我的文件列表作为grep的文件?我认为,由于文件列表不会被shell处理,因此转义和参数长度不再是问题。问题是grep是否支持这种操作模式,我在文档中找不到。

据我所知,支持这一点。您有几个选项。

使用bash循环解析您的文件列表

这是@fedorqui提供的解决方案

 读取文件时;做
grep$ PATTERN$ file
完成< file_with_list_of_files

使用xargs将多个文件同时传递给grep



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

  PATTERN = '^ $'#寻找空行
xargs -n 10 -d'\\\
'grep$ PATTERN< file_with_list_of_files

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



如上所述,但使用空终止行

  PATTERN ='^ $'#寻找空行
tr'\\\
''\0'file_with_list_of_files
xargs -n 10 -0 grep$ PATTERN< file_with_list_of_files

编辑:正确处理空白
编辑2:删除产生乱码输出的示例,添加处理换行符的示例


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 ...

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

  • 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.

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 does not support this as far as I know. You have a few options.

Use a bash loop to parse your file list

This is the solution provided by @fedorqui

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

Use xargs to pass multiple files to grep at once

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

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

Use xargs to pass multiple files to grep at once, dealing with newlines in filenames

As above, but using null-terminated lines

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

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

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

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