Sample Header Ad - 728x90

Why is ag printing blank lines from this file?

1 vote
1 answer
486 views
I want to use [ag](https://github.com/ggreer/the_silver_searcher/blob/master/doc/ag.1.md) 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?
Asked by Andrew (113 rep)
Aug 10, 2016, 06:46 AM
Last activity: Aug 10, 2016, 10:54 PM