I'm trying to develop a better understanding of the command line by working my way through the book "The Linux Command Line" by Schotts, available to view here
http://linuxcommand.org/tlcl.php
I'm on the section dealing with regular expressions on page 262. He uses as an example the regular expression that will "only match lines consisting of groups of one or more alphabetic characters separated by single spaces:"
Code:
^([[:alpha:]]+ ?)+$
As an example he uses the grep command
Code:
carranty@carranty-desktop ~ $ echo "This that" | grep -E '^([[:alpha:]]+ ?)+$'
This that
The problem I'm having is that the + command seems not to work. I get the same result if I leave it out
Code:
carranty@carranty-desktop ~ $ echo "This that" | grep -E '^([[:alpha:]] ?)+$'
This that
and say I want to only match lines consisting of one alphanumeric character followed by a space. I would think
Code:
^([[:alpha:]]? ?)+$
would do the job (I've replaced the + with a ?), but it doesn't
Code:
carranty@carranty-desktop ~ $ echo "This that" | grep -E '^([[:alpha:]]? ?)+$'
This that
it still returns the line even though it has more than one character before the space.
Weirdly, the third quantifier (the first ? in the original code) works because if I pass a line with more than one space
Code:
carranty@carranty-desktop ~ $ echo "This that" | grep -E '^([[:alpha:]]+ ?)+$'
carranty@carranty-desktop ~ $
It doesn't output the line. Can someone please help me out and explain what I'm doing wrong.
Bookmarks