Re: Slightly OT: How to grep for two different things in a file

From: Andreas Kusalananda Kähäri <andreas.kahari_at_abc.se>
Date: Thu, 08 Sep 2022 06:40:05 UTC
On Thu, Sep 08, 2022 at 07:33:50AM +0100, Steve O'Hara-Smith wrote:
> On Thu, 8 Sep 2022 00:56:09 +0200
> Andreas Kusalananda Kähäri <andreas.kahari@abc.se> wrote:
> 
> > 	find src/java -type f \
> > 		-exec grep -qF 'tid' {} \; \
> > 		-exec grep -qF '/tmp' {} \; \
> > 		-print
> 
> 	This is the best solution.
> 
> -- 
> Steve O'Hara-Smith <steve@sohara.org>

One could change that last "grep" into

	-exec grep -lF '/tmp' {} +

... and drop the "-print".  That would make it more efficient, only
calling "grep" once on each file first, and then in bulk on the ones
that contain the first pattern.  If one knows what pattern is expected
to match the most files, that could be arranged to be the pattern used
in the first "grep" ("grep -q" is efficient and will terminate as soon
as the first match is found).

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.