Boolean searches of file content.

Mike Meyer mwm at mired.org
Sun Jan 12 07:44:15 CET 2003


news at youplusme.com (newsman) writes:

> > Say I want to find only those files that have  both  "apple" and
> > "orange" in them.  The command
> > 
> >     egrep 'apple|orange' {directory}
> > 
> > finds either one. Is there a way to do this with egrep? Or is something
> > like "perl" or "sed" better?  I want to end up with a list of files and,
> > if possible, the lines and line numbers where matches were found.
> > 
> > What would be great is some way of being able to specify how close the
> > words were and other details.
> > 
> > Thanks very much for any help.
> 
> How about :
>   ls | egrep 'apple' | egrep 'orange'

That lists the lines that have both apple and orange. And in this
case, you can use fgrep or grep instead of egrep.

If you don't care if the words appear on the same line or not, one of
the magic incantations is:

        grep -l apple * | xargs grep -l orange

And it will list the files that have both apple and orange in them.

And what is this doing in comp.lang.p*?

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.




More information about the Python-list mailing list