计算文件中的空白行数 [英] Count number of blank lines in a file

查看:98
本文介绍了计算文件中的空白行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

中计数(非空白)代码行bash ,他们解释了如何计算非空行的数量.

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.

推荐答案

另一种方法是:

grep -cvP '\S' file

  • -P '\S'(perl正则表达式)将匹配包含非空格的任何行
  • -v选择不匹配的行
  • -c打印匹配行数
    • -P '\S'(perl regex) will match any line contains non-space
    • -v select non-matching lines
    • -c print a count of matching lines
    • 如果您的grep不支持-P选项,请使用-E '[^[:space:]]'

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

      这篇关于计算文件中的空白行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆