tags:

views:

52

answers:

2

I'm using grep to filter the Mac OS X dictionary words file (by default located at /usr/share/dict/words).

I want to use grep to retrieve all words four characters long. How do I do this?

My best guess for how to do this was:

grep [:alpha:]{4} words

But that returns zero results.

+6  A: 

It should be:

grep -E '^[[:alpha:]]{4}$' words

We anchor it (since a 5-letter string also contains a 4-letter string), and POSIX character classes must be contained in a bracket expression ([]). Also, we quote for the shell. And it needs to be extended, so -E.

Matthew Flaschen
Sorry, I forgot the -E.
Matthew Flaschen
+1. Good stuff. Same without -E: `grep '^[[:alpha:]]\{4\}$' words`
Adam Bernier
Your modified answer works, but Aakash's solution is slightly more succinct so I'm giving the accept to him. Thanks for your help. Upvotes all round.
Brian
@Brain, yes, it's more succinct, but it does something different. `[:alpha:]` matches letters, `.` matches any character except NUL.
Matthew Flaschen
+3  A: 

Trying to remember how much of regex language grep supports... does

grep '^....$' words

work for you? Note that since you are searching a dictionary file, I'm not sure you need to restrict yourself to letters.

AakashM