What is the difference between \u and \U in GNU sed

Viewed 247

I come across these two commands \u and \U (and others such as \l and \L) in sed. I have to admit I am a newbie and have little experience with GNU sed. I have tried the following two commands but got the same result:

# I have tested this on Ubuntu 20.04
echo "abc" | sed 's/./\u&/g' # output is: ABC
echo "abc" | sed 's/./\U&/g' # output is: ABC

The output is the same for the two commands. So, what is the difference between them?

1 Answers

In a substitution replacement, \u converts the next character to uppercase, whereas \U converts the rest of the replacement to uppercase, or until \L or \E occurs.

Your example will not show the difference between them, because you are only replacing one character at a time. If you use the pattern .* (instead of .) in echo abc | sed 's/.*/\u&/', you will get Abc.

The commands are documented in info sed, and here: https://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command

Related