问题描述
看起来这很简单,但是我遇到了麻烦.
Seems like this is rather simple, but I'm having trouble.
我有一个看起来像这样的文本文档:
I have a text document that looks, for example, like this:
我想解析此文档,并且仅匹配仅由大写字母组成的整个单词,如下所示:
What I would like is to parse this document and match only whole words made up of only uppercase letters, like so:
我写了这个:
grep -o "\w[[:upper:]]\w" Untitled.txt
这很接近,但是,返回此:
This gets pretty close but, alas, returns this:
...坦率地说,我听不懂.
...which, candidly, I don't understand.
所以:我可能会丢失什么? egrep在OS X下不能很好地工作,因为我受到FreeBSD的grep(grep(BSD grep)2.5.1-FreeBSD)的限制,我想,我为egrep找到的许多解决方案似乎都可以工作没有按预期进行.
So: what might I be missing? egrep doesn't work very well under OS X because I'm limited by FreeBSD's grep (grep (BSD grep) 2.5.1-FreeBSD), I guess, so many of the solutions I've found for egrep that seem like they would work don't work as expected.
推荐答案
您错过了*
,而且\w
是任何单词字符.正确的正则表达式为:
You miss *
and also \w
is any word character. Correct regexp is:
\<[[:upper:]][[:upper:]]*\>
\<
\>
匹配单词边界
这篇关于grep整个单词仅由大写字母组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!