ggreer / the_silver_searcher

A code-searching tool similar to ack, but faster.
http://geoff.greer.fm/ag/
Apache License 2.0
26.08k stars 1.42k forks source link

Add option to read a list of input files from a file? #1461

Open mipuha opened 3 years ago

mipuha commented 3 years ago

Usecase in short: among codebase(s) I want to find python files that contain three different keywords.

ag --py -l -Q keyword1 > files
ag --read-files=files -l -Q keyword2 > files
...
ag --read-files=files -l -Q keywordN 

Some tools have a similar option to read the search space from a separate file. Is this something ag could have some day? Is there another way I could achieve this today?

lewisbrown commented 2 years ago

Better yet, just receive file args on STDIN, then you could pipe it all together:

ag --py -l -Q keyword1 | ag --py -l -Q keyword2 | ag --py -l -Q keywordN

Still better, allow the above functionality without piping:

ag --py -l -Q -e keyword1 -e keyword2 -e keywordN

using random "-e" option for this.

joachim-n commented 2 years ago

Agreed that an option to read a file list from STDIN would be really useful. It would mean that you can pipe multiple ag's to achieve complex searches.

E.g. files that contain foo but not bar, or files that contain foo and bar.

BTW, ack has this:

-x Read the list of files to search from STDIN.

DanKaplanSES commented 2 years ago

Agreed that an option to read a file list from STDIN would be really useful. It would mean that you can pipe multiple ag's to achieve complex searches.

I'm not connecting the dots on this. Can you use an example of what that command line would look like to pull this off?

joachim-n commented 2 years ago

Suppose I want the list of all files which contain 'foo' but NOT 'bar'. With ack's -x option, I can do:

ack -l foo | ack -x -L bar

which breaks down as:

You can chain as much as you like to achieve more complex conditions.

Some things you could do with a regex with lookaheads, but that's assuming the relative order of the two strings is always the same in all files, and also the regexes would get pretty nasty pretty quickly. Piping simple conditions together is much simpler to understand.

joachim-n commented 2 years ago

Here's another example I had today.

In my Drupal site's config folder I have lots of files named wsd_test.test_type.*.yml. I want to search within the ones that contain the string 'wsd_commons', and I want to see in those what they say after the string 'config'.

The following gets me that:

ag -G wsd_test.test_type wsd_commons -l | ack -x -A5 config

I had to use ack for the final part for its -x flag. It breaks down as: