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.
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.
One way using
grep
:Or with whitespace:
Using Perl one-liner:
(On OSX the Perl expressions are not available, -P option)
grep -cx '\s*' file
or
grep -cx '[[:space:]]*' file
That is faster than the code in Steve's answer.
You can also use
awk
for this:From the manual, "The variable NF is set to the total number of fields in the input record". Since the default field separator is the space, any line consisting in either nothing or some spaces will have
NF=0
.Then, it is a matter of counting how many times this happens.
Test
Now let's' count the number of blank lines:
To count how many useless blank lines your colleague has inserted in a project you can launch a one-line command like this:
This prints: