Search files on linux containing a given pattern

The linux utility grep is great. But somehow I do not like it. Too much to remember and regex format sucks.

I wrote a python script which does something similar. This is how one should use this script

./sniff.py path pattern_to_search [file_pattern]

First argument path is the directory in which we want to search. It will search recursively inside dir. Second argument pattern_to_search is a regular expression which we want to search in a file. This regex in defined in python re library. The . matches newline alos.

Third argument file_pattern is optional. This is regular expression of filename. Only those files which matches this regex will be considered. For example if I want to search python files with extension py containing Pool( followed by Adaptor, I do the following,

./sniff.py . "Pool\(.*?Adaptor"  .*py
./Demos/snippets/cubeMeshSigNeur.py:146 
./Demos/snippets/testSigNeur.py:259 
./python/moose/multiscale/core/mumbl.py:206 
./Demos/snippets/multiComptSigNeur.py:268 

And voila, it generates path of matches files and line no at which the match was found. If more than one match was found, then each line no will be appended to filename.

Enhanced by Zemanta
Advertisements