Search code examples
regexmacosbashgrepbsd

grep whole words made of only uppercase letters


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

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

This is a
TEXT DOCUMENT with
SOME capitalized words
BUT NOT all of them are
ALL CAPS
iPhone

What I would like is to parse this document and match only whole words made up of only uppercase letters, like so:

TEXT DOCUMENT
SOME
BUT NOT
ALL CAPS

I wrote this:

grep -o "\w[[:upper:]]\w" Untitled.txt

This gets pretty close but, alas, returns this:

TEX
DOC
UME
SOM
BUT
NOT
ALL
CAP
iPh

...which, candidly, I don't understand.

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.


Solution

  • You miss * and also \w is any word character. Correct regexp is:

    \<[[:upper:]][[:upper:]]*\>
    

    \< \> match word boundaries