tags:

views:

31

answers:

3

I have a file called file.txt with say 3 words

ban
moon
funny

Now I want to match only the words with 3 or 4 characters

grep "[a-z]\{3,4\}" file.txt

This is not working..it stil matches all the 3 words, I was expecting only to match the first 2. What am I doing wrong here?

A: 

That matches every three or four letter combination. What you want is to match every three or four letter combination that is bounded by whitespace or the start or end of a line.

Scott Saunders
Yeah, had to use \< \> to take care of whitespace, begin/end of line
sunny8107
+2  A: 

Use this:

grep "\<[a-z]\{3,4\}\>" file.txt
Marcelo Cantos
Thanks..that did it.
sunny8107
A: 

A word with 5 characters matches [a-z]{4}. You need word boundaries, and egrep:

chris$ egrep "\b[a-z]{3,4}\b" regextest.txt 
ban 
moon 
chris$
chryss