本文介绍了grep 仅由大写字母组成的整个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来这很简单,但我遇到了麻烦.

Seems like this is rather simple, but I'm having trouble.

我有一个看起来像这样的文本文档:

I have a text document that looks, for example, like this:

这是一个
带有
的文本文档一些大写的单词
但并非所有这些都是
全部大写
iPhone

我想要的是解析此文档并仅匹配仅由大写字母组成的整个单词,如下所示:

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:

TEX
文件
UME
索姆
但是
不是
全部

哲学博士

...坦率地说,我不明白.

...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 仅由大写字母组成的整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:10
查看更多