Count source code lines with grep

31 Jul 2009  in the early evening  Matt Winckler

Here’s a handy way to count lines in C# source files using grep and wc, excluding (most) comments and blank lines:

grep -Pr --include=*.cs "^.+$" TargetDirectory | grep -v "^\s*//" | wc -l

Note, however, that this expression still counts comment blocks enclosed in /* */…my grep-fu (and level of caring) evaporated once I recognized that fact, and the expression above got me close enough. If you’re using Visual Studio and have that option turned on that prefixes each extended comment line with an asterisk, you could modify the second expression slightly to become ^(\s*//)|(\s*\*) and that should grab them.

Based on this calculation, I estimate that I’ve written about 60 lines per hour on a side project I’ve been working on this spring/summer. I’m not sure whether that’s good, bad, or (most likely) meaningless, which is about par for the course as far as statistics are concerned.

React

You must be logged in to post a comment.