I want a regular expression that will help me identify if a certain pattern is present as a mixed case in a sentence or not.


Fox juMPed the rock

Now, I want to find that if the pattern jumped has any small letters in it or not. How can I achieve this?

Let me make it a little clearer. What I want is to make the keywords in a code file turn in UPPERCASE. For that I need to identify the keywords which are not already in uppercase.

Like Replaceshould ideally be REPLACE.

But to find such cases, I should be able to identify whether a word has any lowercase letter present or not.

  • grep -i You should be able to see what -i does from man grep command.
    – Ramesh
    Nov 5, 2014 at 5:15
  • Do you mean that you want to be able to match 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 a i modifier for the regex that does just that.
    – Joseph R.
    Nov 5, 2014 at 5:15
  • so you want to find that your pattern i.e jumped has lowercase in sentence or not???
    – Hackaholic
    Nov 5, 2014 at 5:27
  • 1
    Why do you care if it matches an already uppercased word? A word which is already uppercased, converted to uppercase, is still uppercase.
    – phemmer
    Nov 5, 2014 at 5:43

5 Answers 5

printf %s\\n 'juMPEd'| sed 's/[^[:lower:]]//g'


  • This gives me just what I need, to see if a word contains any small letters.
    – haldar55
    Nov 5, 2014 at 5:57

Perl can do this fairly easily:

$ echo 'Fox juMPed the rock' | perl -pe 's/jumped/\U$&/i'
Fox JUMPED the rock

It looks for jumped, case insensitive, and replaces it with the uppercase version.


You can use grep -ioE to obtain all matches for a given regexp

$ echo "abc jumped def Jumped fgh JUMPED klm" > file.txt
$ grep -ioE "j\w+" file.txt > matches.txt
$ cat matches.txt

Now, you can iterate on the result and remove full lower/upper matches (bash) :

$ for w in $(cat matches.txt); do if [[ ! $w == ${w^^} && ! $w == ${w,,} ]]; then echo $w; fi; done > mixed.txt
$ cat mixed.txt

For each mixed word, you can invoke sed to uppercase the given match

$ for w in $(cat mixed.txt); do sed -i "s:$w:${w^^}:g" file.txt; done

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:]]'

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}'
  • That last operation should work on any version of sed. Just have to use \< \> for the word boundary instead of \b.
    – phemmer
    Nov 5, 2014 at 5:51
  • @Patrick Thanks for that: I updated the answer to use \< and \>.
    – John1024
    Nov 5, 2014 at 5:53
  • Its a bit complicated than that, not only do I have to change the keywords into UPPERCASE, but I would also have to keep a track as in where I made the change etc.
    – haldar55
    Nov 5, 2014 at 5:56
  • @haldar55 OK. I updated the answer with a method that looks through text (code) to isolate lower or mixed case appearances of jumped.
    – John1024
    Nov 5, 2014 at 6:05
  • the -o option in grep is not available to me
    – haldar55
    Nov 5, 2014 at 13:58

This can be done entirely within the shell, with a case construct.

string='Fox juMPed the rock'
case "$string" in
  *[:lower:]*) echo "The string contains lowercase letters.";;

In ksh, you can use typeset -u to convert a string to uppercase. It doesn't matter whether the original was all-uppercase or not.

string='Fox juMPed the rock'
typeset -u string
echo "SHOUTING: $string"

If you want to process a file, you can use awk and its toupper function to convert a string to uppercase. You can test if s is already uppercase with s == toupper(s), though you might as well use toupper(s) regardless.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .