Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Sep 2022 00:06:20 +0100
From:      Miguel C <miguelmclara@gmail.com>
To:        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:  <CADGo8CW0=_j2zsC-Hryh8scaxZ-0Oxep9H0tY2DJm6RNzWZ5ug@mail.gmail.com>
In-Reply-To: <YxkhieQ2omb3sr5s@harpo.local>
References:  <CAGBxaXn6ZO-e0746fwzNp%2Bv-6bAucjxePMOt-mEv2HKmkCBXcg@mail.gmail.com> <YxkhieQ2omb3sr5s@harpo.local>

next in thread | previous in thread | raw e-mail | index | archive | help
--0000000000004980af05e81e5d27
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Maybe I didn't understand the complexity here but doesn't grep -E or egrep
work here ?

egrep "string1|string2" ?

Also works with -r and you can use it inside exec in find too... but if you
want to search for more that one string AFAIK this is the easiest way.



On Wed, Sep 7, 2022, 23:56 Andreas Kusalananda K=C3=A4h=C3=A4ri <andreas.ka=
hari@abc.se>
wrote:

> On Wed, Sep 07, 2022 at 06:00:36PM -0400, 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
>
> I don't see an example of the stuff you talk about after "But since
> /tmp is in a symbolic constant defined elsewhere..." so I don't fully
> understand what that would involve and will therefore ignore it.
> Instead, the following answers the subject question, "How to grep for
> two different things in a file".
>
>         find src/java -type f \
>                 -exec grep -qF 'tid' {} \; \
>                 -exec grep -qF '/tmp' {} \; \
>                 -print
>
> or call an in-line script,
>
>         find src/java -type f -exec sh -c '
>                 for pathname do
>                         if grep -qF "tid" "$pathname" &&
>                            grep -qF "/tmp" "$pathaname"
>                         then
>                                 printf "%s has both tid and /tmp\n"
> "$pathname"
>                         fi
>                 done' sh {} +
>
> In any case, the point is to first test a file for one of th strings,
> and if that succeeds, test the same file for the other string, then
> report the file as accepted if that other string was also found.
>
> See grep(1) for what -F and -q does.  I dropped the -i option as I
> assumed that you actully know the case, at least when looking for
> "/tmp".
>
> Also, https://unix.stackexchange.com/questions/389705
>
>
> --
> Andreas (Kusalananda) K=C3=A4h=C3=A4ri
> SciLifeLab, NBIS, ICM
> Uppsala University, Sweden
>
> .
>
>

--0000000000004980af05e81e5d27
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">Maybe I didn&#39;t understand the complexity here but doe=
sn&#39;t grep -E or egrep work here ?<div dir=3D"auto"><br></div><div dir=
=3D"auto">egrep &quot;string1|string2&quot; ?</div><div dir=3D"auto"><br></=
div><div dir=3D"auto">Also works with -r and you can use it inside exec in =
find too... but if you want to search for more that one string AFAIK this i=
s the easiest way.=C2=A0</div><div dir=3D"auto"><br></div><div dir=3D"auto"=
><br></div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"g=
mail_attr">On Wed, Sep 7, 2022, 23:56 Andreas Kusalananda K=C3=A4h=C3=A4ri =
&lt;<a href=3D"mailto:andreas.kahari@abc.se">andreas.kahari@abc.se</a>&gt; =
wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8e=
x;border-left:1px #ccc solid;padding-left:1ex">On Wed, Sep 07, 2022 at 06:0=
0:36PM -0400, Aryeh Friedman wrote:<br>
&gt; I have 2 patterns I need to find in a given set of files.=C2=A0 A file=
 only<br>
&gt; matches if it contains *BOTH* patterns but not in any given<br>
&gt; relationship as to where they are in the file.=C2=A0 =C2=A0In the past=
 I have<br>
&gt; used piped greps when both patterns are on the same line but in my<br>
&gt; current case they are almost certainly not on the same line.<br>
&gt; <br>
&gt; For example my two patterns are &quot;tid&quot; (String variable name)=
 and<br>
&gt; &quot;/tmp&quot; [String literal] (i.e. the full string is the concate=
nation of<br>
&gt; the two patterns I would do:<br>
&gt; <br>
&gt; grep -Ri tid src/java|grep -i /tmp<br>
&gt; <br>
&gt; But since /tmp is in a symbolic constant defined elsewhere (in a<br>
&gt; different Java file) I need to find programmatically either the name<b=
r>
&gt; of the constant (has different names in different classes) and then do=
<br>
&gt; the piped grep above with it or I need to look for the two patterns<br=
>
&gt; separately and say a file is only accepted if it has both.<br>
&gt; <br>
&gt; P.S. The reason for this is I am attempting to audit my code base to<b=
r>
&gt; see what classes leave behind orphaned temp files.<br>
&gt; <br>
&gt; -- <br>
&gt; Aryeh M. Friedman, Lead Developer, <a href=3D"http://www.PetiteCloud.o=
rg" rel=3D"noreferrer noreferrer" target=3D"_blank">http://www.PetiteCloud.=
org</a><br>
<br>
I don&#39;t see an example of the stuff you talk about after &quot;But sinc=
e<br>
/tmp is in a symbolic constant defined elsewhere...&quot; so I don&#39;t fu=
lly<br>
understand what that would involve and will therefore ignore it.<br>
Instead, the following answers the subject question, &quot;How to grep for<=
br>
two different things in a file&quot;.<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 find src/java -type f \<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -exec grep -qF &#39=
;tid&#39; {} \; \<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -exec grep -qF &#39=
;/tmp&#39; {} \; \<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -print<br>
<br>
or call an in-line script,<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 find src/java -type f -exec sh -c &#39;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for pathname do<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 if grep -qF &quot;tid&quot; &quot;$pathname&quot; &amp;&amp;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0grep -qF &quot;/tmp&quot; &quot;$pathaname&quot;<br=
>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 then<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 printf &quot;%s has both tid and /tm=
p\n&quot; &quot;$pathname&quot;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 fi<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 done&#39; sh {} +<b=
r>
<br>
In any case, the point is to first test a file for one of th strings,<br>
and if that succeeds, test the same file for the other string, then<br>
report the file as accepted if that other string was also found.<br>
<br>
See grep(1) for what -F and -q does.=C2=A0 I dropped the -i option as I<br>
assumed that you actully know the case, at least when looking for<br>
&quot;/tmp&quot;.<br>
<br>
Also, <a href=3D"https://unix.stackexchange.com/questions/389705" rel=3D"no=
referrer noreferrer" target=3D"_blank">https://unix.stackexchange.com/quest=
ions/389705</a><br>
<br>
<br>
-- <br>
Andreas (Kusalananda) K=C3=A4h=C3=A4ri<br>
SciLifeLab, NBIS, ICM<br>
Uppsala University, Sweden<br>
<br>
.<br>
<br>
</blockquote></div>

--0000000000004980af05e81e5d27--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CADGo8CW0=_j2zsC-Hryh8scaxZ-0Oxep9H0tY2DJm6RNzWZ5ug>