beyondgrep / ack3

ack is a grep-like search tool optimized for source code.
https://beyondgrep.com/
Other
712 stars 66 forks source link

Add grep-like -f option to read pattern from a file #219

Open Zer0-Tolerance opened 5 years ago

Zer0-Tolerance commented 5 years ago

title says it all :)

petdance commented 5 years ago

Say more about how you would use this. What's the use case for reading the pattern from a file? What are you doing now to achieve this functionality?

n1vux commented 5 years ago

fgrep -f is commonly used to searching for an OR of multiple fixed strings, without upgrading to grep and '|" and without reprocessing a list into a | expression.

(It is possible to do egrep -f for multiple REs one per line but that is not the common usecase.)

For the fgrep -f usecase, fgrep will always be faster than ack.
Having a file listing a bunch of perl REs would be leveraging ack's value added, but I've seen so few examples of (e)grep -f that weren't really fgrep -f aka grep -F -f I'm unconvinced.

This is a long-standing exception to our drop-in support for (ef)grep commandflags (we have -f for "what files match?" long-since).

The Perl workaround if someone really really needs to use ack with a file listing fixed strings or Perl REs is

ack "$(perl -000 -pE 'chomp; s/\012/\174/g;' $pattern_file)"  $other_args

which could be packaged into an alias ack-f to use the first arg to Perl and rest of args to ack.

Zer0-Tolerance commented 5 years ago

One typical use case on my side is to be able to use a list of RE from a file to look for a malicious pattern. More specifically it would be great to be able to capture the match with $1 as well as the name of the file. Why not using fgrep or egrep ? because I also want to do this to run on windows without installing outdated unix binaries ... I like the workaround idea of ack-f , is this something you can implement soon ?

petdance commented 5 years ago

Is this something you'd be using from a script, or as part of an interactive session?

Would ack --pattern-from-file patterns.txt basically do an OR for all the patterns that it finds in the file?

In any case this isn't something I would be working on any time soon.

n1vux commented 5 years ago

I am pleased to see you using Ack for defensive security. :-D I note that you are desiring a large OR of Perl REs (PCREs) for that -- that is not the usual case of [ef]grep -f (although supported as egrep -Pf in latest GNUish egrep!!). For your use, that makes excellent sense, and you're trying to keep your prerequisites sane, ditto.

I like the workaround idea of ack-f , is this something you can implement soon ?

the commandline I provided above

ack "$(perl -000 -pE 'chomp; s/\012/\174/g;' $pattern_file)"  $other_args

could be the basis of an Alias definition on Linux/Unix, or could be the basis for both CMD and *SH scripts for Windows and LInux/etc. OTOH, you could run the inner portion to preprocess your pattern file into single-line RE form.

perl -000 -pE 'chomp; s/\012/\174/g;' $patterns_file > $pattern_file

On the other other hand, we will happily acknowledge sometimes Ack isn't the best tool for the job. TIMTOWTDI is our Perl motto! The More Tools page and Comparison page give a broad shallow and narrow deep coverage of alternatives. For your use-case, Ack compatibility, speed, and -f --file support like Grep suggests rg RipGrep might be a good choice, if hauling around a Rust runtime isn't more trouble than hauling around Perl. (OTOH if you need Perl for other things, as I would, Rust is added burden.)

n1vux commented 5 years ago

For later reference, per Comparison page, if and when we support --pattern-from-file patterns.txt we should support as its short form the long form supported by Gnu (e)grep and RipGrep, rg --file patterns.txt .