Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Sep 2022 07:56:40 +0300
From:      Mehmet Erol Sanliturk <m.e.sanliturk@gmail.com>
To:        Michael Schuster <michaelsprivate@gmail.com>
Cc:        Carl Johnson <carlj@peak.org>, freeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   Re: Slightly OT: How to grep for two different things in a file
Message-ID:  <CAOgwaMsz6uogrLHTcRqN2iR3cBLv4iNVdrj15gsieUfKLP3hAQ@mail.gmail.com>
In-Reply-To: <CADqw_gJH2vaAtG6sfmdbhcfditXx8d2dJBden9Y6kONrNhKtoQ@mail.gmail.com>
References:  <CAGBxaXn6ZO-e0746fwzNp%2Bv-6bAucjxePMOt-mEv2HKmkCBXcg@mail.gmail.com> <86edwmmsyu.fsf@bay.localnet> <CADqw_gJH2vaAtG6sfmdbhcfditXx8d2dJBden9Y6kONrNhKtoQ@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
On Thu, Sep 8, 2022 at 7:45 AM Michael Schuster <michaelsprivate@gmail.com>
wrote:

> On Thu, Sep 8, 2022 at 5:43 AM Carl Johnson <carlj@peak.org> wrote:
> >
> > Aryeh Friedman <aryeh.friedman@gmail.com> writes:
> >
> > > 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.
>
> I put together a simple awk-version (on linux, but there shouldn't be
> much difference to BSD) - consider this a start, not a solution:
> --- begin multigrep.awk
> /foo/ {foo_line=NR; if (bar_line > 0) { printf "%d; %d\n", foo_line,
> bar_line; exit } }
> /bar/ {bar_line = NR; if (foo_line > 0) { printf "%d; %d\n", foo_line,
> bar_line; exit } }
> --- end multigrep.awk
>
> HTH
>
> > >
> > > 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.
> >
> > I use grep -l to just return a list of files that contain one pattern,
> > and then grep -l for the second pattern on that list.  That can be done
> > in one line for your example as follows:
> >
> >   grep -li /tmp `grep -liR tid src/java`
> >
> > I hope that gives you some ideas.
> > --
> > Carl Johnson            carlj@peak.org
> >
>
>
> --
> Michael Schuster
> http://recursiveramblings.wordpress.com/
> recursion, n: see 'recursion'
>
>

During my program development , I am using the technique defined by Carl
Johnson
for more than thirty years .

At the beginning I was using grep but it was not very useful .

I have developed my own search program to make my multiple successive
searches for
AND requiring phrases by generating and then utilizing file lists  .

Michael Schuster's suggestion is also very good  if intermediate file lists
are not required .


With my best wishes .


Mehmet Erol Sanliturk

[-- Attachment #2 --]
<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif;font-size:large"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Sep 8, 2022 at 7:45 AM Michael Schuster &lt;<a href="mailto:michaelsprivate@gmail.com">michaelsprivate@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Thu, Sep 8, 2022 at 5:43 AM Carl Johnson &lt;<a href="mailto:carlj@peak.org" target="_blank">carlj@peak.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Aryeh Friedman &lt;<a href="mailto:aryeh.friedman@gmail.com" target="_blank">aryeh.friedman@gmail.com</a>&gt; writes:<br>
&gt;<br>
&gt; &gt; I have 2 patterns I need to find in a given set of files.  A file only<br>
&gt; &gt; matches if it contains *BOTH* patterns but not in any given<br>
&gt; &gt; relationship as to where they are in the file.   In the past I have<br>
&gt; &gt; used piped greps when both patterns are on the same line but in my<br>
&gt; &gt; current case they are almost certainly not on the same line.<br>
<br>
I put together a simple awk-version (on linux, but there shouldn&#39;t be<br>
much difference to BSD) - consider this a start, not a solution:<br>
--- begin multigrep.awk<br>
/foo/ {foo_line=NR; if (bar_line &gt; 0) { printf &quot;%d; %d\n&quot;, foo_line,<br>
bar_line; exit } }<br>
/bar/ {bar_line = NR; if (foo_line &gt; 0) { printf &quot;%d; %d\n&quot;, foo_line,<br>
bar_line; exit } }<br>
--- end multigrep.awk<br>
<br>
HTH<br>
<br>
&gt; &gt;<br>
&gt; &gt; For example my two patterns are &quot;tid&quot; (String variable name) and<br>
&gt; &gt; &quot;/tmp&quot; [String literal] (i.e. the full string is the concatenation of<br>
&gt; &gt; the two patterns I would do:<br>
&gt; &gt;<br>
&gt; &gt; grep -Ri tid src/java|grep -i /tmp<br>
&gt; &gt;<br>
&gt; &gt; But since /tmp is in a symbolic constant defined elsewhere (in a<br>
&gt; &gt; different Java file) I need to find programmatically either the name<br>
&gt; &gt; of the constant (has different names in different classes) and then do<br>
&gt; &gt; the piped grep above with it or I need to look for the two patterns<br>
&gt; &gt; separately and say a file is only accepted if it has both.<br>
&gt; &gt;<br>
&gt; &gt; P.S. The reason for this is I am attempting to audit my code base to<br>
&gt; &gt; see what classes leave behind orphaned temp files.<br>
&gt;<br>
&gt; I use grep -l to just return a list of files that contain one pattern,<br>
&gt; and then grep -l for the second pattern on that list.  That can be done<br>
&gt; in one line for your example as follows:<br>
&gt;<br>
&gt;   grep -li /tmp `grep -liR tid src/java`<br>
&gt;<br>
&gt; I hope that gives you some ideas.<br>
&gt; --<br>
&gt; Carl Johnson            <a href="mailto:carlj@peak.org" target="_blank">carlj@peak.org</a><br>
&gt;<br>
<br>
<br>
--<br>
Michael Schuster<br>
<a href="http://recursiveramblings.wordpress.com/" rel="noreferrer" target="_blank">http://recursiveramblings.wordpress.com/</a><br>;
recursion, n: see &#39;recursion&#39;<br>
<br></blockquote><div><br></div><div><br></div><div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">During my program development , I am using the technique defined by Carl Johnson <br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">for more than thirty years .</div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">At the beginning I was using grep but it was not very useful .</div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">I have developed my own search program to make my multiple successive searches for</div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">AND requiring phrases by generating and then utilizing file lists  .</div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">Michael Schuster&#39;s suggestion is also very good  if intermediate file lists are not required .</div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">With my best wishes .</div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default">Mehmet Erol Sanliturk</div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif;font-size:large" class="gmail_default"><br></div><br></div><div><br></div><div><br></div><div> </div></div></div>

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