[sf-lug] regex: how to match any one to four character word in a file

jim jim at well.com
Wed Dec 10 10:12:57 PST 2008

from charles-henri (using an email address not 
registered on the sf-lug mailing list): 

jim wrote:
        > i've given up on the online tutorials. 
        > i have a text file with over 100000 words (and lines, 
        > one word per line). i wanna grep out all words that 
        > are from one to four characters, e.g. 'a' or 'and' 
        > or "fact" but not "apple" or "zounds". 
        > $  grep '[.]{4}' words.txt
        > got me a newline. 
[.] will match a literal '.'
'{' needs to be escaped. \{$4\} will match exactly 4
Also, you need to anchor your regex (with ^ and $)

grep '^.\{1,4\}$'


More information about the sf-lug mailing list