Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using Ubuntu 13.10 for a while now, programming in C++ and Python. I have some question that I can solve by making a program, but maybe there are easier ways, so here is the first question.

I can use grep to find lines with a word/pattern and then use wc to count them:

grep word somefile | wc -l

How do I count the lines that have no "text"? Those that are empty or have only spaces or tabs.

share|improve this question
up vote 8 down vote accepted

Your system should have GNU grep, that has an option -P to use Perl expressions and you can use that, combined with -c (so no need for wc -l):

grep -Pvc '\S' somefile

The '\S' hands the pattern \S to grep and matches all line containing anything that is not space, -v selects all the other lines (those only with space), and -c counts them.

From the man page for grep:

-P, --perl-regexp
       Interpret  PATTERN  as  a  Perl  regular  expression  (PCRE, see
       below).  This is highly experimental and grep  -P  may  warn  of
       unimplemented features.

-v, --invert-match
       Invert the sense of matching, to select non-matching lines.  (-v
       is specified by POSIX.)

-c, --count
       Suppress normal output; instead print a count of matching  lines
       for  each  input  file.  With the -v, --invert-match option (see
       below), count non-matching lines.  (-c is specified by POSIX.)
share|improve this answer
If you don't want to use the 'highly experimental' -P or don't have gnu grep, then grep -Ec '^[[:space:]]*$' somefile should work (works the same way). I have +1'd anyway as who doesn't have gnu grep these days... – abligh Feb 9 '14 at 12:46
@abligh, MacOS, *BSD, propietary Unices, ... – vonbrand Feb 9 '14 at 14:36
Sure (that's why I gave the alternative). OS-X grep does have -P and has done since (at least) 10.6. – abligh Feb 9 '14 at 16:19

You can use grep for it:

$ grep -c '^[     ]*$' somefile

where ^/$ match the start/end of a line and [ ] specifies the character class that includes space and tab characters. * denote 0 to unlimited repetitions of the left hand side.

The -c option tells grep to count the lines matching the pattern - instead of printing them to the terminal.

Note that on zsh I have to hit Ctrl+VTab to enter a literal tab character.

(a [ \t] is not recognized by grep)

share|improve this answer
Personally, I find Timo's solution much cleaner. This requires you to type those characters manually. But here's a +1! – Amal Murali Feb 9 '14 at 15:39
@AmalMurali, it's a matter of taste, of course - but perhaps not everybody is familiar (or wants to be familiar) with all of Perl's regex extensions ... (not to talk about grep versions without -P support ...) – maxschlepzig Feb 9 '14 at 16:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.