silver_searcher (ag) with multiple search expressions?
Asked Answered
M

3

11

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

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

Materialize answered 11/1, 2017 at 15:27 Comment(0)
L
9

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" * 
Lowerclassman answered 11/1, 2017 at 18:14 Comment(0)
W
17

You might want to search with both Boolean operators:

  • AND: ag -l pattern1 | xargs ag -l pattern2 | xargs ag 'pattern1|pattern2'
  • 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.

Whirlpool answered 18/5, 2018 at 4:43 Comment(1)
Add -d '\n' to xargs to handle spaces on filenames. That's a really useful tip. I usually try to mess with -0. What do you do if the path has parentheses or ~ in it?Midden
L
9

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" * 
Lowerclassman answered 11/1, 2017 at 18:14 Comment(0)
S
6

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

ag 'pattern1|pattern2'
Sherysherye answered 11/1, 2017 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.