简体   繁体   中英

How can I use a file with a list of paths to multiple files as my grep's input file to search in?

I have file called file.txt with hundreds of paths to a file in each line.

file.txt:

dir1/a.txt
dir2/b.txt
and so on

I'm trying to use this file as an input to my grep command using this:

less file.txt | xargs -I{} grep -E -A5 'search|pattern' {} 

but it's saying file name too long, and it seems to think that the 'a|b|c' is part of the filename based on the error but I can't tell what I'm doing wrong with the current code.

Also my search pattern is very long since I need to find a lot of things from those specific files listed in file.txt.

Any help is appreciated, thanks.

  • use cat get the strings of the file & for loop to iterate through them:
[root@stack]#cat files
dir1/file1
dir2/file2
dir3/file3

[root@stack]#for file in $(cat files); do grep 'foo' $file; done
foo
foo
foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM