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 "name.of.some.show.s03e11.finale.avi" 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 name.of.some.show.s03e11.finale.avi | sed -r 's/([s][0-9]{2}[e])/\U\1/g'
name.of.some.show.S03E11.finale.avi

2. Transform the first letter to uppercase

echo name.of.some.show.s03e11.finale.avi| sed -r 's/^./\U&/'
Name.of.some.show.s03e11.finale.avi

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

echo name.of.some.show.s03e11.finale.avi |  sed -r 's/\../\U&/g'
name.Of.Some.Show.S03e11.Finale.Avi

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'
name.Of.Some.Show.S03e11.Finale.avi

All together now:

echo name.of.some.show.s03e11.finale.avi | \
sed -r '
  s/([s][0-9]{2}[e])/\U\1/g
  s/^./\U&/
  s/\../\U&/g
  s/\..{1,5}$/\L&/g
'
Name.Of.Some.Show.S03E11.Finale.avi

Leave a comment

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