Count the number of empty lines in a file using grep

Recently I had a need to count the number of empty lines in a text file. After some digging into the man pages of grep I was able to come up with a one liner which was able to do it.

Following my tradition of documenting one liners, I am going to document this one as well :)

Assumption

By empty line, I mean any line which either has no characters or has only whitespace (space, tab) characters.

Command

For the impatient in you, here is the actual command.

Explanation

  • -P '\S' – This selects all lines that have a non whitespace character
  • -c – Print the count of matching lines
  • -v – Select only the non-matching lines

So, we are first matching all lines that have a non whitespace character and then use -v option to ignore them and then -c option to print the count instead of the actual line.

If we wanted the count of all non-empty lines, then we just have to remove the -v option from the above command.

Hope this is helpful. Happy Grep’ing ;)

Related posts

Tags: , ,

5 Comments so far

Trackback URI | Follow up comments through RSS Feed | Post a comment

  • anand says:

    Hey Sudar,

    The notification mail of this post had the command completely stripped off.

    • Sudar says:

      Thanks for reporting this. Can you kindly forward the mail to me. I am using feedburner and I think it is getting into some issues.

      May be time to replace it with a custom solution.

  • Like anand said above, the subscription mail stripped off the one-liner command.

    BTW, why not `grep -c -e ‘^\s*$’ filename` ?

    -c for counting the number of matches
    -e to let grep know that a regular expression follows

    The literal meaning of regular expression used in the above solution is “to match if a start of line, following by a whitespace or tab for any number of times, including zero times, followed by the end of line”.

    Also, which version of grep you use? On a mac with BSD grep (2.5.1-FreeBSD), the ‘-P’ is not implemented. In Ubuntu with GNU grep (2.10), your solution works, though.

    • Sudar says:

      Like anand said above, the subscription mail stripped off the one-liner command.

      Kindly forward the email. I will have a look at it. Not sure why it is getting stripped.

      BTW, why not `grep -c -e ā€˜^\s*$ā€™ filename` ?

      There is absolutely no reason to not use it. It is just that I found the other one first :)

      Also, which version of grep you use? On a mac with BSD grep (2.5.1-FreeBSD), the ā€˜-Pā€™ is not implemented.

      I am using 2.5.1 (GNU grep) in Mac Lion. `-P` works for me, but I guess I would have upgraded to GnU grep sometime back.

      • Kindly forward the email. I will have a look at it. Not sure why it is getting stripped.

        Done.

        Also forwarded another issue with the comment subscription via another email (the email that I use for comment subscription is different from the email used for blog subscription).

1 Trackbacks/Pingbacks so far

Leave a Reply