How do you replace the first letter of a word into Capital letter, e.g.
Trouble me
Gold rush brides
into
Trouble Me
Gold R
Use the following sed command for capitalizing the first letter of the each word.
echo -e "Trouble me \nGold rush brides" | sed -r 's/\<./\U&/g'
output
Trouble Me
Gold Rush Brides
The -r
switch tells sed
to use extended regular expressions. The instructions to sed
then tell it to "search and replace" (the s
at the beginning) the pattern \<.
with the pattern \U&
globally, i.e. all instances in every line (that's the g
modifier at the end). The pattern we're searching for is \<.
which is looking for a word boundary (\<
) followed by any character (.
). The replacement pattern is \U&
, where \U
instructs sed
to make the following text uppercase and &
is a synonym for \0, which refers to "everything that was matched". In this case, "everything that was matched" is just what the .
matched, as word boundaries are not included in the matches (instead, they are anchors). What .
matched is just one character, so this is what is upper cased.