Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

silver_searcher (ag) with multiple search expressions?

Does silver_searcher support specifying multiple search expressions something like -e in grep?

I could not find any option in the document/help.

like image 815
justrajdeep Avatar asked Jan 11 '17 15:01

justrajdeep


3 Answers

You might want to search with both Boolean operators:

  • AND: ag -l pattern1 | xargs ag -l pattern2 | xargs ag 'pattern1|pattern2'
    • Add -d '\n' to xargs to handle spaces on filenames.
  • OR: ag 'pattern1|pattern2'
  • NOT: ag -v 'pattern'

From manual:

-l --files-with-matches: Only print the names of files containing matches, not the matching lines. An empty query will print all files that would be searched.

-v --invert-match: Match every line not containing the specified pattern.

like image 183
Pablo Bianchi Avatar answered Nov 13 '22 04:11

Pablo Bianchi


According to the documentation, it doesn't support multiple search patterns. That said, it does support using parallel, so you can fire off multiple instances of ag for a multi-search:

echo "foo\nbar\nbaz" | parallel 'ag --parallel --color "{}" *' 

The output using the --parallel switch will be filename, linenumber and match. If that's too fancy, you can always use the OR operator in your pattern search:

ag --color "foo|bar|baz" * 
like image 44
gregory Avatar answered Nov 13 '22 04:11

gregory


Yes, you can search for multiple patterns by separating each pattern with a vertical line character (|):

ag 'pattern1|pattern2'
like image 42
Chad Nouis Avatar answered Nov 13 '22 03:11

Chad Nouis