Count number of blank lines in a file

2019-03-10 16:18发布

In count (non-blank) lines-of-code in bash they explain how to count the number of non-empty lines.

But is there a way to count the number of blank lines in a file? By blank line I also mean lines that have spaces in them.

7条回答
你好瞎i
2楼-- · 2019-03-10 17:17

Another way is:

grep -cvP '\S' file
  • -P '\S'(perl regex) will match any line contains non-space
  • -v select non-matching lines
  • -c print a count of matching lines

If your grep doesn't support -P option, please use -E '[^[:space:]]'

查看更多
登录 后发表回答