Let me make it a little clearer. What I want is to make the keywords
in a code file turn in UPPERCASE.
In that case, there is no need to test if a word has any lower case letters. It is easier and, in the end, likely more efficient to just replace them all.
$ echo Fox Jumped and JUMPED and juMPed the rock | sed 's/jumped/JUMPED/gi'
Fox JUMPED and JUMPED and JUMPED the rock
Refinement: Match only whole words
If you like, you can insist that only whole words be replaced:
sed 's/\<jumped\>/JUMPED/gi'
How to identify any occurrence of an identifier that is not fully upper case
Using grep -o
$ echo Fox Jumped and JUMPED and juMPed the rock | grep -woi jumped | grep '[[:lower:]]'
Jumped
juMPed
Two grep
commands are used. The first does a case-insensitive search for the whole word jumped
and the second restricts the matches to those with at least one lower case letter.
Using awk
This awk
command will look for any occurrence of word in mixed-case or lower case:
awk -v 'RS=[^[:alnum:]]' 'tolower($0)=="jumped" && $0 ~ /[[:lower:]]/ {print $0}'
Here, words are defined to consist of alphanumeric characters. If your words allow more characters, adjust RS
accordingly.
As an example:
$ echo Fox Jumped and JUMPED and juMPed the rock | awk -v 'RS=[^[:alnum:]]' 'tolower($0)=="jumped" && $0 ~ /[[:lower:]]/ {print $0}'
Jumped
juMPed
grep -i
You should be able to see what-i
does fromman grep
command.jumped
case-insensitively; that is any possible permutation of the case of its individual characters should match? If yes, whichever tool you're using will probably have ai
modifier for the regex that does just that.