Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Sep 2022 08:12:39 +0200
From:      Andreas Kusalananda =?utf-8?B?S8OkaMOkcmk=?= <andreas.kahari@abc.se>
To:        Jaskaran Veer Singh <lists@jaskaran.org>
Cc:        Aryeh Friedman <aryeh.friedman@gmail.com>, FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   Re: Slightly OT: How to grep for two different things in a file
Message-ID:  <YxmH15ODU/CM/P1i@harpo.local>
In-Reply-To: <CMQKET7J77ZC.JF6EL4PJ7K6N@eniac.local>
References:  <CAGBxaXn6ZO-e0746fwzNp%2Bv-6bAucjxePMOt-mEv2HKmkCBXcg@mail.gmail.com> <CMQKET7J77ZC.JF6EL4PJ7K6N@eniac.local>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Sep 07, 2022 at 07:41:13PM -0400, Jaskaran Veer Singh wrote:
> Maybe awk could help here
> 
> awk '/pattern1/ {next} /pattern2/ {print FILENAME}' somefile.txt
> 
> to translate this: if pattern1 is found, we jump to find the 'next' pattern in
> the file. If that is found as well, we print the FILENAME.
> 
> You can use this on all files using a bit of shell kungfu:
> 
> for f in *; do awk '<whatever here>' "$f"; done
> 
> --
> Jaskaran Veer Singh

You would have to keep track of how many times each pattern has matched,
and then exit immediately when both have been found.

	awk '
	    /pattern1/ { count1++ }
	    /pattern2/ { count2++ }
	    count1 && count2 { exit }
	    END { exit !(count1 && count2) }'  file

Here, the count variables keep track of the number of times each pattern
has been matched (note, the matching is by extendend regular expression,
not string comparisons, which may matter) and if both are non-zero the
code exits.  The END block will be executed when the first "exit" is
called, or by reaching the end of the file, and will return an exit
status to the shell in a manner similar to "grep -q".


> 
> On Wed Sep 7, 2022 at 6:00 PM EDT, Aryeh Friedman wrote:
> > I have 2 patterns I need to find in a given set of files.  A file only
> > matches if it contains *BOTH* patterns but not in any given
> > relationship as to where they are in the file.   In the past I have
> > used piped greps when both patterns are on the same line but in my
> > current case they are almost certainly not on the same line.
> >
> > For example my two patterns are "tid" (String variable name) and
> > "/tmp" [String literal] (i.e. the full string is the concatenation of
> > the two patterns I would do:
> >
> > grep -Ri tid src/java|grep -i /tmp
> >
> > But since /tmp is in a symbolic constant defined elsewhere (in a
> > different Java file) I need to find programmatically either the name
> > of the constant (has different names in different classes) and then do
> > the piped grep above with it or I need to look for the two patterns
> > separately and say a file is only accepted if it has both.
> >
> > P.S. The reason for this is I am attempting to audit my code base to
> > see what classes leave behind orphaned temp files.
> >
> > --
> > Aryeh M. Friedman, Lead Developer, http://www.PetiteCloud.org
> 

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

.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?YxmH15ODU/CM/P1i>