开发者

Fast search for text in files in a directory in unix?

Is there a good solution for performing searches similar to

开发者_运维知识库
find . -name "*.*" | xargs grep "some text"

but with much faster search, due to offline indexing. Support for wildcards or light regular expressions would be nice, but even raw text search that could run very fast due to offline preprocessing would be great.


I think this is a great and fast option

grep -rl "string" /path


This might be overkill for your purposes, but Beagle allows you to perform very fast searches of local files. It's usually marketed as a desktop application, but in fact it is just a daemon that can respond to requests from the command-line using beagle-query.


Recoll has a decent indexing mechanism for local files, but it comes with a GUI.


grep -Ri --include="*.xml" TEXT /directory/

  • this searches 'TEXT' text in all xml files in particular directory, but it may not be as fast as you expect


Python

from __future__ import print_function
import glob
for name in glob.glob('*.*'):
    with open(name,'r') as aFile:
        for n, text in enumerate(aFile):
            if 'some text' in text:
                print( name, n, text )

You have a very complete regular expression library in the re module.

Rather than print the results, you could create a shelve database or some other index structure.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜