Not sure if the title is accurate but what I really wanted to say is to find a certain string, which file and which line within your project directory no matter how deep the directory level is. This is extremely useful when debugging or searching for a variable, method or class. I name these two scripts as qfind
and rfind
.
qfind
qfind
is a tool that will let you specify a file name extension and the exact string pattern. Then the tool will echo back the file name and the line numbers where the string is found. Below is the simple code for the script.
#!/bin/sh if [ "$1" == "" ]; then echo "Usage: qfind <css|js|php|etc> <str>" exit 1 fi if [ "$2" == "" ]; then echo "Usage: qfind <css|js|php|etc> <str>" exit 1 fi find -regextype posix-extended -regex ".*\.($1)" -type f | xargs grep -n "$2"
Sample usage of the script.
lysender@darkstar:~/www-repo/dclabc1$ qfind py 'data_provider' ./test/dclabtest/handler/testrest.py:74: @decorator.data_provider(method_provider) ./test/dclabtest/handler/testrest.py:80: @decorator.data_provider(is_action_valid_provider) ./test/testdclab.py:45: @decorator.data_provider(date_fail) ./test/testdclab.py:49: @decorator.data_provider(date_fail_format) ./test/testdclab.py:53: @decorator.data_provider(date_pass) ./test/testdclab.py:57: @decorator.data_provider(date_pass_format) ./pylib/decorator.py:1:def data_provider(fn_data_provider): ./pylib/decorator.py:5: for i in fn_data_provider(): lysender@darkstar:~/www-repo/dclabc1$
rfind
rfind
on another hand is a tool that lets you specify a file name extension and a PERL flavor regex pattern to search. It has almost the same behavior as qfind
but instead, it accepts regular expressions for the search pattern. Below is the script code.
#!/bin/sh if [ "$1" == "" ]; then echo "Usage: rfind <css|js|php|etc> <perl-regex>" exit 1 fi if [ "$2" == "" ]; then echo "Usage: rfind <css|js|php|etc> <perl-regex>" exit 1 fi find -regextype posix-extended -regex ".*\.($1)" -type f | xargs grep -n -P "$2"
Sample usage:
lysender@darkstar:~/www-repo/dclabc1$ rfind py "'code': 40(3|4), 'message': '" ./test/dclabtest/handler/testrest.py:283: result = r.generate_response({'code': 404, 'message': 'Record not found'}) ./test/dclabtest/handler/testrest.py:284: expected = {'code': 404, 'message': 'Record not found'} ./test/dclabtest/handler/testrest.py:314: self.assertEquals(json.dumps({'code': 403, 'message': 'Invalid parameters'}), get_call[1][0]) ./test/dclabtest/handler/testrest.py:315: self.assertEquals(json.dumps({'code': 403, 'message': 'Invalid parameters'}), post_call[1][0]) ./dclab/handler/rest.py:38: invalid_param_return = {'code': 403, 'message': 'Invalid parameters'} ./dclab/promoflight/handler/location.py:38: return {'code': 404, 'message': 'Location not found'} ./dclab/promoflight/handler/location.py:52: return {'code': 404, 'message': 'No locations found'} ./dclab/promoflight/handler/location.py:66: return {'code': 403, 'message': 'Invalid parameters'} ./dclab/promoflight/handler/location.py:80: return {'code': 403, 'message': 'Invalid parameters'} ./dclab/promoflight/handler/location.py:93: return {'code': 403, 'message': 'Invalid parameters'} lysender@darkstar:~/www-repo/dclabc1$
Just put them on your PATH and then it is ready to use. Enjoy.
Hi! Please also try out internal code search using the free edition of Code Sight. Let me know how it looks! (susie at blackducksoftware dot com)
Now that’s hard core 😀
Thanks :). Did you have a chance to try it? ( www dot blackducksoftware dot com / code-sight ) Feedback is appreciated!