upper- to lower-case using sed

To change getFoo_Bar to getFoo_bar using sed :

echo "getFoo_Bar" | sed 's/^\(.\{7\}\)\(.\)\(.*\)$/\1\l\2\3/'

The upper and lowercase letters are handled by :

  • \U Makes all text to the right uppercase.
  • \u makes only the first character to the right uppercase.
  • \L Makes all text to the right lowercase.
  • \l Makes only the first character to the right lower case. (Note its a lowercase letter L)

The example is just one method for pattern matching, just based on modifying a single chunk of text. Using the example, getFoo_BAr transforms to getFoo_bAr, note the A was not altered.


s/\(get[A-Z][A-Za-z0-9]*_\)\([A-Z]\)/\1\L\2/g

Test:

$ echo 'getFoo_Bar' | sed -e 's/\(get[A-Z][A-Za-z0-9]*_\)\([A-Z]\)/\1\L\2/g'
getFoo_bar

Tags:

Regex

Sed