简体   繁体   中英

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?

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

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'

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", [^az]+$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM