48

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.

Community
  • 1
  • 1
Sudar
  • 18,954
  • 30
  • 85
  • 131

7 Answers7

69

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:]]'

kev
  • 155,172
  • 47
  • 273
  • 272
  • 2
    This works and is also very fast when compared with other answer, so I am accepting this answer over the other one. – Sudar Nov 22 '12 at 05:08
  • 2
    I also blogged about it at http://sudarmuthu.com/blog/count-the-number-of-empty-lines-in-a-file-using-grep – Sudar May 19 '13 at 08:32
  • 3
    For easy copying for OSX users: `grep -cvE '[^[:space:]]'` – Gregory Arenius Jun 02 '15 at 22:56
  • 2
    On Mac, use `grep -cvE '\S' file` – Hanxue Jan 27 '16 at 11:49
  • With GNU grep `grep -cvE '\S' file` and `grep -cv '\S' file` work, too. Whether they are faster or slower than with `-P`, depends on hardware and grep version. E.g. with grep version 2.25 with Intel(R) Atom(TM) CPU N270 @ 1.60GHz perl regex is slower, and grep version 2.16 with Intel(R) Core(TM)2 CPU 6400 @ 2.13GHz perl regex is faster. – jarno Mar 13 '18 at 15:15
  • `grep -cx '\s*' file` is also pretty fast depending on hardware and grep implementation. See my answer. – jarno Mar 17 '18 at 08:49
27

One way using grep:

grep -c "^$" file

Or with whitespace:

grep -c "^\s*$" file 
Steve
  • 51,466
  • 13
  • 89
  • 103
  • Thanks this works. But I am going to choose the other answer, since it was very fast. – Sudar Nov 22 '12 at 05:01
  • I can't understand why one should be faster than the other one and can't think of a material to test the difference, can you please explain or give numbers? I do think both should give the exact same efficiency. And as this answer is more straightforward, I think it's the best one. The other one use a double negation : invert match on none space character. – Adrien Horgnies Feb 22 '16 at 23:33
  • @AdrienHorgnies use e.g. https://www.ietf.org/download/rfc-index.txt as test file, and use `time` to measure the execution time. – jarno Mar 13 '18 at 14:26
  • @AdrienHorgnies see also my answer. – jarno Mar 13 '18 at 16:01
  • @AdrienHorgnies Implementation of grep and even hardware used may affect relative efficiency of the commands as you can see in my comment to kev's answer. Also the amount of complexity of pattern may affect it; using some options instead of writing more complex pattern may be more efficient, if the options are implemented more efficiently than interpreting of the pattern. – jarno Mar 13 '18 at 19:17
3

You can also use awk for this:

awk '!NF {sum += 1} END {print sum}' file

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

$ cat a
aa dd

ddd


he      llo
$ cat -vet a # -vet to show tabs and spaces
aa dd$
    $
ddd$
   $
^I$
he^Illo$

Now let's' count the number of blank lines:

$ awk '!NF {s+=1} END {print s}' a
3
fedorqui
  • 275,237
  • 103
  • 548
  • 598
2

Using Perl one-liner:

perl -lne '$count++ if /^\s*$/; END { print int $count }' input.file
Socowi
  • 25,550
  • 3
  • 32
  • 54
Majid Azimi
  • 5,575
  • 13
  • 64
  • 113
2
grep -v '\S' | wc -l

(On OSX the Perl expressions are not available, -P option)

Roy
  • 31
  • 1
2

grep -cx '\s*' file

or

grep -cx '[[:space:]]*' file

That is faster than the code in Steve's answer.

jarno
  • 787
  • 10
  • 21
1

To count how many useless blank lines your colleague has inserted in a project you can launch a one-line command like this:

blankLinesTotal=0; for file in $( find . -name "*.cpp" ); do blankLines=$(grep -cvE '\S' ${file}); blankLinesTotal=$[${blankLines} + ${blankLinesTotal}]; echo $file" has" ${blankLines} " empty lines."  ; done; echo "Total: "${blankLinesTotal}

This prints:

<filename0>.cpp #blankLines
....
....
<filenameN>.cpp #blankLines
Total #blankLinesTotal
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146