views:

395

answers:

4

A popular text editor has the following "find in files" feature that opens in a dialog box:

 Look For:       __searchtext__
 File Filter:    *.txt; *.htm
 Start From:     c:/docs/2009
 Report:         [ ] Filenames [ ]FileCount only
 Method:         [ ] Regex     [ ]Plain Text

In fact, several popular text editors have this.

I would like to do the same thing but using a python or ruby class instead of a text editor. That way, this same kind of brain-dead simple operation can be run from a script on any platform that supports ruby or python.

Question: I don't feel like writing this myself, so does anyone know of a ruby or python script that accepts the same or similar easy input args and does what you'd expect?

I am looking for something that does a brute-force linear search, nothing to do with indexed searches.

+1  A: 

If you'll expand your languages, take a look at ack at http://betterthangrep.com/

Andy Lester
+3  A: 

I know you said you don't feel like writing it yourself, but for what it's worth, it would be very easy using os.walk - you could do something like this:

results = []
if regex_search:
    p = re.compile(__searchtext__)
for dir, subdirs, subfiles in os.walk('c:/docs/2009'):
    for name in fnmatch.filter(subfiles, '*.txt'):
        fn = os.path.join(dir, name)
        with open(fn, 'r') as f:
            if regex_search:
                results += [(fn,lineno) for lineno, line in enumerate(f) if p.search(line)]
            else:
                results += [(fn,lineno) for lineno, line in enumerate(f) if line.find(__searchtext__) >= 0]

(that's Python, btw)

David Zaslavsky
Haven't you forgotten about the filename filter?
Bartosz Radaczyński
just add one line before the os.path.join using the standard library fnmatch module, e.g. `if not fnmatch.fnmatch(name, '*.txt'): continue` or the like.
Alex Martelli
@Bartosz: yeah, I did... thanks Alex for the fix.
David Zaslavsky
+3  A: 

Grepper is a Ruby gem by David A. Black for doing exactly that:

g = Grepper.new
g.files = %w{ one.txt two.txt three.txt }
g.options = %w{ B2 }   # two lines of before-context
g.pattern = /__search_string__/
g.run

g.results.each do |file, result|
  result.matches.each do |lineno, before, line, after|
    etc....

I believe it shells out to grep and wraps the results in Ruby objects, which means it takes the same options as grep. Install with:

sudo gem install grepper
Daniel Lucraft
+1  A: 

A good series of articles about "grep in Python" and related subjects, by Muharem Hrnjadovic, is at http://muharem.wordpress.com/2007/05/16/python-file-find-grep-and-in-line-replace-tools-part-1/ and following articles in the same blog.

Alex Martelli