Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep for only uppercase words

Tags:

linux

grep

bash

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?

like image 729
Jasmine Lognnes Avatar asked Oct 18 '25 12:10

Jasmine Lognnes


1 Answers

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
like image 163
anubhava Avatar answered Oct 21 '25 02:10

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!