Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

NOTE: I have updated this question from what I posted earlier and hence some answers may not match with what I intend to ask.

I want to search some TEXT in a set of files in sub-directories and simultaneously want to skip results from certain files having "svn" in their filenames.

How can I accomplish that?

share|improve this question
2  
Could you please show some more specific example of what exactly you want to accomplish? Match only lines matching one pattern but not another or what? –  Andreas Wiese Apr 18 at 10:31
    
@AndreasWiese : Updated the question –  Don't You Worry Child Apr 18 at 10:54
1  
Rather than update, perhaps you should rewrite, including the title, so that it's clear from the first read? Why retain the unclear parts just to correct them later? As it stands, this is almost an XYProblem. –  user61786 Apr 18 at 11:19
    
@awk_FTW : Done!! :) –  Don't You Worry Child Apr 18 at 11:35
    
You may find ack useful. It is a programmer friendly grep that understands many versioning systems. –  Gowtham Apr 18 at 16:21

3 Answers 3

up vote 2 down vote accepted

The details will depend on what exactly you want to do, but negative lookarounds might help. Certain regular expression languages, including PCRE support this. You can activate PCREs in grep with the -P switch, so depending on the specific pattern you are looking for, you might be able to do something clever with these but it is very unlikely to be worth the effort.

Having read your update however, there are much better ways of doing it. grep offers the -exclude option:

   --exclude=GLOB
          Skip  files  whose  base  name  matches  GLOB  (using   wildcard
          matching).   A  file-name  glob  can  use  *,  ?,  and [...]  as
          wildcards, and \ to quote  a  wildcard  or  backslash  character
          literally.

So, you could simply do

grep -Inr TEXT --exclude='*.svn' dir/
share|improve this answer
    
The only problem with using lookarounds is that look-behinds need to be a fixed length, so they can't be used to do anything generically. –  Graeme Apr 18 at 11:23
1  
@Graeme I know, it's trivial if the string to be avoided is adjacent to the pattern but gets complicated otherwise. That's why I didn't give an example, not really worth the effort. –  terdon Apr 18 at 11:27

There's no way to do both a positive search (grep) and a negative search (grep -v). Alternative would be to use -E (or egrep, which is same thing) and provide a matching regular expression that would match one string but not match the other.

share|improve this answer
    
That will simply ignore both strings. –  terdon Apr 18 at 10:33
    
Yeah that was a bad call, I have updated it to provide a more generic answer –  edvinas.me Apr 18 at 10:35
    
Better :) Still, the -E is not needed, you can provide a regular expression to normal grep. -E just uses ERE instead of BRE. –  terdon Apr 18 at 10:40
    
Thanks for the "regular expression" method. –  Don't You Worry Child Apr 18 at 10:56
    
@MadHatter that's grep's default behavior. It always expects a regular expression as a search string. -E (like -P) simply change the regex language used. –  terdon Apr 18 at 10:58

In case you discover that you need more sophisticated control of the files you are grepping than is offered by --exclude, you can use find to produce the list of files for grep:

$ grep 'whatever_you_want_to_find' $(find wherever_you_want_to_search -not -iregex 'whatever_you_want_to_ignore')

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.