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

前端 未结 19 1506
耶瑟儿~
耶瑟儿~ 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:53

    'wc' counts lines, words, chars, so to count all lines (including blank ones) use:

    wc *.py
    

    To filter out the blank lines, you can use grep:

    grep -v '^\s*$' *.py | wc
    

    '-v' tells grep to output all lines except those that match '^' is the start of a line '\s*' is zero or more whitespace characters '$' is the end of a line *.py is my example for all the files you wish to count (all python files in current dir) pipe output to wc. Off you go.

    I'm answering my own (genuine) question. Couldn't find an stackoverflow entry that covered this.

提交回复
热议问题