Search code examples
c++cmetricslines-of-code

Simple script to count NLOC?


Do you know a simple script to count NLOCs (netto lines of code). The script should count lines of C Code. It should not count empty lines or lines with just braces. But it doesn't need to be overly exact either.


Solution

  • I would do that using awk & cpp (preprocessor) & wc . awk removes all braces and blanks, the preprocessor removes all comments and wc counts the lines:

    find . -name \*.cpp -o -name \*.h | xargs -n1 cpp -fpreprocessed -P | 
        awk '!/^[{[:space:]}]*$/' | wc -l
    

    If you want to have comments included:

    find . -name \*.cpp -o -name \*.h | xargs awk '!/^[{[:space:]}]*$/' | wc -l