yatsek
yatsek

Reputation: 1015

Is it possible to force silver searcher to search through files on list or chain searches?

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

Answers (3)

Raz Luvaton
Raz Luvaton

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

Iwnnay
Iwnnay

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

gregory
gregory

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

Related Questions