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]
The first argument, path
, is the directory in which we will search recursively. The second argument, pattern_to_search
, is a regular expression which we want to search in a file. We use the regular expression format defined in the Python re
library. In this script, the .
also matches newline.
The third argument, file_pattern
, is optional. This is another regular expression which works on a filename. Only those files which matches this regular expression will be considered.
For example, if I want to search Python files with the extension py
containing Pool(
followed by word 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 the path of matched files and line number at which the match was found. If more than one match was found, then each line number will be appended to the filename.