Reputation: 1015
To print only names of files which match search there is:
ag -l
What should I use if I want to grep only through files from list? Or maybe is it possible to chain searches in this way?
Upvotes: 3
Views: 1936
Reputation: 3790
All you need to do is run:
ag <pattern> <file1> <file2> <...fileN>
Example:
$ ag 'todo' a b
a
59: // todo - demo 1
92: // todo - demo 2
b
15: // TODO - demo 3
Upvotes: 0
Reputation: 2008
From this page I gleamed both the fact that the ag -l
returns a list of file names and that ag
takes a stdin of files. That got me thinking about another way that has worked out pretty great for me. Wanted to share
ag -l <first_search> | xargs ag -l <second_search> | xargs ag -l ...
Upvotes: 4
Reputation: 12973
Use std input redirection:
ag "SEARCH_PATTERN" < file1, file,2, file3...
Or use a wild card search for target files with the -g option. The template would look like this:
ag -g "FILE_PATTERN" "SEARCH_PATTERN"
So an example would be as follows:
ag -G '.*.txt' "pattern"
Or feed files or search patterns into ag from a .txt file containing a list of entries, using a shell's for loop; for example:
while IFS= read -r patterns; do ag -o "$patterns" ~/somewhere ; done < names.txt
Upvotes: 1