简体   繁体   中英

How do I grep for all words that are less than 4 characters?

我有一个字典,单词用换行符分隔。

You can just do:

egrep -x '.{1,3}' myfile

This will also skip blank lines, which are technically not words. Unfortunately, the above reg-ex will count apostrophes in contractions as letters as well as hyphens in hyphenated compound words. Hyphenated compound words are not a problem at such a low letter count, but I am not sure whether or not you want to count apostrophes in contractions, which are possible (eg, I'm). You can try to use a reg-ex such as:

egrep -x '\w{1,3}' myfile

..., but this will only match upper/lower case letters and not match contractions or hyphenated compound words at all.

像这样: grep -v "^...." my_file

试试这个正则表达式:

grep -E '^.{1,3}$' your_dictionary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM