Python Forum
a useful grep command - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Forum & Off Topic (https://python-forum.io/forum-23.html)
+--- Forum: Bar (https://python-forum.io/forum-27.html)
+--- Thread: a useful grep command (/thread-14870.html)



a useful grep command - Skaperen - Dec-21-2018

i would like to see a grep command that can search python source files and limit the search to just the specified part of the source, ignoring these parts when not selected:

1. comments

2. string literals

3. bytes literals

4. code


RE: a useful grep command - Larz60+ - Dec-21-2018

try using sed
see: https://linuxcommando.blogspot.com/2008/03/using-sed-to-extract-lines-in-text-file.html


RE: a useful grep command - Gribouillis - Dec-21-2018

Quote:that can search python source files and limit the search to just the specified part of the source,
Using python's tokener, it is pretty easy to rewrite a python file by removing a subset of these categories. For example you could replace these tokens by white space, then search with regex in the remaining part. There are pure python grep packages such as 'grin', probably others in pypi.


RE: a useful grep command - Skaperen - Dec-21-2018

(Dec-21-2018, 11:02 AM)Larz60+ Wrote: try using sed
see: https://linuxcommando.blogspot.com/2008/03/using-sed-to-extract-lines-in-text-file.html
the idea is to lex or parse the file enough to distinguish parts like string literals, comments, and code and apply the search to the specified parts. so if i specify to search code and literals, it won't search the comments (even within the same line). sed does not have any capability to understand python source.