I want to use ag to print the classes and their methods in a python file. I thought that this would be easy using:
ag --context=0 --nocolor -os '^\s*(def|class)\s+[_A-Za-z]*' prog.py but for reasons I don't understand this is also matching blank lines. For example, if you make prog.py the following
class MyFavouriteClass def __init__ def __contains__ blah class MyNextFavouriteClass def _repr_ def __iter__ then it returns the full file, including the blank lines, except for the line containing blah. Of course, I can always pipe the output into something else to remove the blank lines but I'd rather get it right the first time.
I suspect that the problem has nothing to do with the regular expression and, instead, that it's a feature of ag's --context, --after and --before flags but I can't find a combination of these that does what I want.
Any ideas?
--context=1give you one line before and after matches.--context=0and it still gives me the blank lines. The--context=1slipped in after a few edits. You're right in that I should have kept--context=0in the question -- now fixed. Unfortunately, this doesn't solve my problem.