the_silver_searcher icon indicating copy to clipboard operation
the_silver_searcher copied to clipboard

How to search for multiple terms in a file?

Open ylluminate opened this issue 6 years ago • 2 comments

I'm looking for a way to use ag to search for multiple keywords in files. For example, I would like to grep through all files within a folder that have both terms present anywhere in the file (in this case "head" and "qt" from the Homebrew Formula folder).

Is there a clean way to use an and operator as such? I though that there may be other solutions as in other here, however it even appears that these commands (such as awk '/pattern1/ && /pattern2/' *) only match via and per line and not the entire file...

ylluminate avatar Sep 23 '17 18:09 ylluminate

It appears that this will do the trick, but perhaps there's a more efficient method: ag -l pattern1 * | tr '\n' '\0' | xargs -0 ag pattern2

ylluminate avatar Sep 23 '17 19:09 ylluminate

Here is a workaround for pattern1 AND pattern2 on ag (here on SO).

Would be great o have it builtin on ag, though.

pabloab avatar May 07 '21 04:05 pabloab