Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a list of files to grep

Tags:

grep

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.

like image 262
blueFast Avatar asked Nov 05 '13 15:11

blueFast


1 Answers

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

like image 107
dwurf Avatar answered Oct 07 '22 00:10

dwurf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!