Reputation: 7097
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?
Upvotes: 4
Views: 6448
Reputation: 241988
If you don't want the \w
in the output, don't include it in the pattern.
grep -oP '[A-Z]+' *
To get the expected output, though, you need to include underscores and word boundaries:
grep -oP '\b[A-Z_0-9]+\b'
If you want to avoid ____
and similar (are they common in Python code?), use
grep -oP '\b[A-Z_0-9]*[A-Z][A-Z_0-9]*\b'
Upvotes: 2
Reputation: 785551
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
Upvotes: 8
Reputation: 2829
The regex thinks S
, A
and T
are uppercase words. So this depends on how you define uppercase word. From your examples, it appears you're looking for something more like ^[A-Z_]+$
. Or, if by "uppercase word" you mean "no lowercase characters", [^a-z]+$
Upvotes: 0