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

99封情书 提交于 2021-02-07 09:59:18

问题


I have a dictionary with words separated by line breaks.


回答1:


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 (e.g., 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.




回答2:


Like this: grep -v "^...." my_file




回答3:


Try this regular expression:

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


来源:https://stackoverflow.com/questions/4982052/how-do-i-grep-for-all-words-that-are-less-than-4-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!