Match text with regular expressions and transform upper and lower case letters with sed

Sed can be used to selectively match parts of a line of text using regular expressions as well as transform letters to/from upper or lower case. I've found examples on the net of people using "\U" and "\u" to capitalize case, but didn't see any mention of this in the sed manual. It only mentions use of "y" to transliterate characters. But since "\U" transforms to upper case just fine, I tried "\L" and that transforms to lower case just fine as well. Interesting.

Anyway, let's say I have a string like "" and I wanted it to be "Name.Of.Some.Show.S03E11.Finale.avi".

Here's one way to do it with sed:

1. Transform letters "s" (as in [s]eason) and "e" (as in [e]pisode) to uppercase:

echo | sed -r 's/([s][0-9]{2}[e])/\U\1/g'

2. Transform the first letter to uppercase

echo| sed -r 's/^./\U&/'

3. Transform every letter immediately preceded by a period to uppercase

echo |  sed -r 's/\../\U&/g'

4. Step 3 will also capitalize the file extension, which we'd like to always be lower case, so we transform the 1 to 5 letters at the end of the string to lowercase:

echo name.Of.Some.Show.S03e11.Finale.Avi | sed -r 's/\..{1,5}$/\L&/g'

All together now:

echo | \
sed -r '

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).