Jasmine Lognnes Jasmine Lognnes - 4 months ago 16
Linux Question

grep for only uppercase words

I would like to find all words that all all uppercase, but when I do

grep -oP '\w*[A-Z]+\w*' *


I get

words.py:StringValue
words.py:WORDS
words.py:WORDS_ANSWERED
words.py:Answered
words.py:True


where I were hoping for

words.py:WORDS
words.py:WORDS_ANSWERED


Question

How can I make sure that only all uppercase words is outputted?

Answer

You can use this regex with word boundary on either side and by using [A-Z0-9_] instead of \w:

grep -H -oP '\b[A-Z0-9_]*[A-Z]+[A-Z0-9_]*\b' *

words.py:WORDS
words.py:WORDS_ANSWERED
Comments