good5dog5
good5dog5

Reputation: 149

Is silver-searcher able to obtain PATTERN from file?

There are 84 PATTERN need to be check, i store them in file name pattern.txt.

Is silver-searcher (also named Ag) able to obtain these patterns from pattern.txt?

grep has -f options to read pattern from file, but the man page of silver-searcher mention nothing about it.

Upvotes: 4

Views: 805

Answers (2)

Jürgen Hötzel
Jürgen Hötzel

Reputation: 19747

Joining the lines in the pattern file to create a regex group:

ag  "($(paste -sd "|" pattern.txt))" .

Upvotes: 1

gregory
gregory

Reputation: 12973

No, there isn't a similar -f option in ag. The simple approach is to use loop to pass the patterns to ag; for instance you could use a while loop to read the patterns like this:

while read pattern; do ag "$pattern" -G '.*.txt' ; done < patterns.txt

I suggest the faster approach of using GNU parallel with ag. Parallel and ag work very well together:

< patterns.txt | parallel 'ag --filename --parallel --color "{}" '

Here, I'm passing each pattern to parallel which in turn spawns a number of ag processes which search for their own pattern matches. Parallel is somewhat smart about how many processes to start, but you can tweak it to your heart's content (https://www.gnu.org/software/parallel/man.html). In short, you'll rip through your 84 patterns far faster with parallelization.

Upvotes: 2

Related Questions