views:

1426

answers:

3

I usually use the following pipeline to grep for a particular search string and yet ignore certain other patterns:

grep -Ri 64 src/install/ | grep -v \.svn | grep -v "file"| grep -v "2\.5" | grep -v "2\.6"

Can this be achieved in a succinct manner? I am using GNU grep 2.5.3.

+5  A: 

Just pipe your unfiltered output into a single instance of grep and use an extended regexp to declare what you want to ignore:

grep -Ri 64 src/install/ | grep -v -E '(\.svn|file|2\.5|2\.6)'

Edit: To search multiple files maybe try

find ./src/install -type f -print |\
    grep -v -E '(\.svn|file|2\.5|2\.6)' | xargs grep -i 64

Edit: Ooh. I forgot to add the simple trick to stop a cringeable use of multiple grep instances, namely

ps -ef | grep something | grep -v grep

Replacing that with

ps -ef | grep "[s]omething"

removes the need of the second grep.

HTH

cheers,

Rob Wells
The parenthesizes must be quoted .. otherwise bash complains: syntax error near unexpected token `('
Sridhar Ratnakumar
Updated! Thanks.
Rob Wells
There is one bug in your solution and the example in my question: the second grep will match the actual line *and* the filename that appears in the output of the first grep. This is good for ignoring `.svn` .. but not other patterns.
Sridhar Ratnakumar
Hmm, not sure what you're saying there but maybe try the update above
Rob Wells
You might want to reverse the order of `grep -v` that removes filenames and the `xargs` so that `xargs` only gets the files you want to grep to begin with.
Nathan Fellman
@Natahn, thanks just updated.
Rob Wells
+1  A: 

You maybe want to use ack-grep which allow to exclude with perl regexp as well and avoid all the VC directories, great for grepping source code.

Chmouel Boudjnah
Thanks for the pointer; for purely Python source tree, I currently use `py.lookup` from http://pylib.org/
Sridhar Ratnakumar
+1  A: 

you can use awk instead of grep

awk '/64/&&!/(\.svn|file|2\.[56])/' file
ghostdog74