Ryan Sisson
Ryan Sisson

Reputation: 139

How do I use a grep command to display lines that only have a space character?

I have a file called test01 it currently contains:

1  Line one.$
2    This is the second line. $
3       The third  $
4  $
5    This    is    really line 4,  with one         blank line before. $
6  $
7  $
8   $
9  Five$
10  $
11 Line 6 is this.
12     Seven $
13 $
14 Eighth real line. $
15 $
16 $
17   Line 9 $
18     Line   10   is   the  last$
19 $
20                      $ 

I need to write a grep command that will only output lines that contain a space character. It shouldn't output lines such as 4 or 6. The desired output should be lines 8, 10 and 20. I've tried grep -vn '[a-z,A-Z,0-9]' test01 however I get the lines that do not contain characters.

Upvotes: 1

Views: 3045

Answers (2)

Cody Piersall
Cody Piersall

Reputation: 8547

grep -n "^ +$" test01

The ^ means "line starts with", then a space with a + sign, which means "one or more spaces", then the $ means line ends with. So it matches only lines with only spaces

Upvotes: 1

falsetru
falsetru

Reputation: 369074

Use pattern ^ +$:

grep -E '^ +$' filename.txt

Use -n if you want to get line number:

$ egrep -E -n '^ +$' filename.txt
8:
10:
20:

Upvotes: 1

Related Questions