I am trying to run grep against a list of a few hundred files:
$ head -n 3 <(cat files.txt)
admin.php
ajax/accept.php
ajax/add_note.php
However, even though I am grepping for a string that I know is found in the files, the following does not search the files:
$ grep -i 'foo' <(cat files.txt)
$ grep -i 'foo' admin.php
The foo was found
I am familiar with the -f
flag which will read the patterns from a file. But how to read the input files?
I had considered the horrible workaround of copying the files to a temporary directory as cp
seems to support the <(cat files.txt)
format, and from there grepping the files. Shirley there is a better way.
while
could receive the lines of file.txt as such. – dotancohen Jan 13 '15 at 09:25while
isn't exactly receiving the lines from the file,read
is doing that;while
just lets us do that in a loop. The loop ends whenread
fails (i.e. returns a non-zero return code), normally due to the End Of File being reached. – PM 2Ring Jan 13 '15 at 10:01IFS= read -r filename
,read filename
is something else. – Stéphane Chazelas Jan 13 '15 at 10:02-H
is a GNU extension. You're missing some--
. – Stéphane Chazelas Jan 13 '15 at 10:03read -r
see http://unix.stackexchange.com/q/18886/88378 and http://unix.stackexchange.com/q/87301/88378 – PM 2Ring Jan 13 '15 at 10:07