count (non-blank) lines-of-code in bash


In Bash, how do I count the number of non-blank lines of code in a project?

9/22/2008 1:20:42 PM

Accepted Answer

cat foo.c | sed '/^\s*$/d' | wc -l

And if you consider comments blank lines:

cat | sed '/^\s*#/d;/^\s*$/d' | wc -l

Although, that's language dependent.

9/22/2008 1:37:39 PM

find . -path './pma' -prune -o -path './blog' -prune -o -path './punbb' -prune -o -path './js/3rdparty' -prune -o -print | egrep '\.php|\.as|\.sql|\.css|\.js' | grep -v '\.svn' | xargs cat | sed '/^\s*$/d' | wc -l

The above will give you the total count of lines of code (blank lines removed) for a project (current folder and all subfolders recursively).

In the above "./blog" "./punbb" "./js/3rdparty" and "./pma" are folders I blacklist as I didn't write the code in them. Also .php, .as, .sql, .css, .js are the extensions of the files being looked at. Any files with a different extension are ignored.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow