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

./ 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,

./ . "Pool\(.*?Adaptor"  .*py

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