[Linux-disciples] AND with grep (fwd)
Stephen R Laniel
steve at laniels.org
Fri Mar 18 09:22:04 EST 2005
I think the following does what I wanted below:
grep -l [term1] [filesToSearch] | xargs -i grep -l [term2] '{}' | [...] | xargs -i grep -l [termN] '{}'
This returns a list of all those files that contain term1
AND term2 AND ... AND termN. I suspect it's the most
efficient way to do so, though I invite improvements.
----- Forwarded message from Adam Kessel <adam at rosi-kessel.org> -----
Date: Thu, 20 May 2004 18:45:06 -0400
From: Adam Kessel <adam at rosi-kessel.org>
To: Community of interest for free software support <linux-disciples at lists.bostoncoop.net>
Subject: Re: [Linux-disciples] AND with grep
User-Agent: Mutt/1.5.5.1+cvs20040105i
Reply-To: Community of interest for free software support <linux-disciples at lists.bostoncoop.net>
On Thu, May 20, 2004 at 04:38:21PM -0400, Stephen R Laniel wrote:
> If you want to find all documents that contain both [term1]
> AND [term2], not necessarily on the same line, here's my
> thought on how to do it:
>
> 1) grep for [term1], assume that the output is of the form
> '[filename]:.*$', and grab all the [filename]s that match
>
> 2) repeat 1) for [term2].
>
> 3) compare the lists obtained from 1) and 2). Return any
> filenames matching both.
>
> Is there a better way?
>
Maybe not a "better" way, but there are other ways.
For example:
cat file | tr "\n" " " | grep "\(term1.*term2\)\|\(term2.*term1\)"
Or
for x in *; do if [ -f $x ]; then cat $x | tr "\n" " " | perl -n -e "/(term1.*term2)|(term2.*term1)/is and print \"$x\n\""; fi; done;
Or the above of as a bash script, i.e.
grepand term1 term2 scope
#!/bin/bash
term1=$1
term2=$2
shift 2
for x in $@; do if [ -f $x ]; then cat $x | tr "\n" " " | perl -n -e "/($term1.*$term2)|($term2.*$term1)/is and print \"$x\n\""; fi; done;
Etc..
I don't know if there's a really graceful way to do it, though.
--
Adam Kessel
http://adam.rosi-kessel.org
_______________________________________________
Linux-disciples mailing list
Linux-disciples at lists.bostoncoop.net
http://shorl.com/jetrepesisihe
----- End forwarded message -----
--
Stephen R. Laniel
steve at laniels.org
+(617) 308-5571
http://laniels.org/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.bostoncoop.net/pipermail/linux-disciples/attachments/20050318/b05f4cfd/attachment.pgp
More information about the Linux-disciples
mailing list