How do you use grep to find terms with a length of n characters?

I use grep to filter Mac OS X dictionary files (located by default in / usr / share / dict / words).

I want to use grep to extract all four character words. How to do it?

My best guess how to do this:

grep [:alpha:]{4} words 

But this returns zero results.

+4
source share
2 answers

Trying to remember how regex grep supported ... does

 grep '^....$' words 

works for you? Please note: since you are looking for a dictionary file, I am not sure that you need to limit yourself to letters.

+9
source

It should be:

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

We bind it (since the 5-letter string also contains the 4-letter string), and the POSIX character classes must be contained in the bracket expression ( [] ). In addition, we provide for the shell. And it needs to be expanded, so -E.

+13
source

Source: https://habr.com/ru/post/1310589/


All Articles