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

前端 未结 19 1493
耶瑟儿~
耶瑟儿~ 2020-12-07 07:14

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

19条回答
  •  生来不讨喜
    2020-12-07 07:52

    If you want the sum of all non-blank lines for all files of a given file extension throughout a project:

    while read line
    do grep -cve '^\s*$' "$line"
    done <  <(find $1 -name "*.$2" -print) | awk '{s+=$1} END {print s}'
    

    First arg is the project's base directory, second is the file extension. Sample usage:

    ./scriptname ~/Dropbox/project/src java
    

    It's little more than a collection of previous solutions.

提交回复
热议问题