Uppercasing First Letter of Words Using SED

LinuxBashUnixAwkSed

Linux Problem Overview


How do you replace the first letter of a word into Capital letter, e.g.

Trouble me
Gold rush brides

into

Trouble Me
Gold Rush Brides

Linux Solutions


Solution 1 - Linux

This line should do it:

sed -e "s/\b\(.\)/\u\1/g"

Solution 2 - Linux

Using awk:

awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1' file

The output would be:

Trouble Me
Gold Rush Brides

Solution 3 - Linux

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.

Solution 4 - Linux

I had apostrophes so, working off the first solution...

mike@mike-laptop3:~$ echo "BEST WESTERN PLUS BOB's INN" | tr "[A-Z]" "[a-z]" | sed -e "s/\b\(.\)/\u\1/g"

Best Western Plus Bob'S Inn

mike@mike-laptop3:~$ echo "BEST WESTERN PLUS BOB's INN" | tr "[A-Z]" "[a-z]" | sed "s/\( \|^\)\(.\)/\1\u\2/g"

Best Western Plus Bob's Inn

Solution 5 - Linux

Another shorter version with sed:

sed -e "s/\b./\u\0/g"

Solution 6 - Linux

I know you said sed, but for shell scripting one of the easiest and more flexible for me was using Python which is available in most systems:

$ echo "HELLO WORLD" | python3 -c "import sys; print(sys.stdin.read().title())"
Hello World

For example:

$ lorem | python3 -c "import sys; print(sys.stdin.read().title())"
Officia Est Omnis Quia. Nihil Et Voluptatem Dolor Blanditiis Sit Harum. Dolore Minima Suscipit Quaerat. Soluta Autem Explicabo Saepe. Recusandae Molestias Et Et Est Impedit Consequuntur. Voluptatum Architecto Enim Nostrum Ut Corrupti Nobis.

You can also use things like strip() to remove spaces, or capitalize():

$ echo "  This iS mY USER ${USER}   " | python3 -c "import sys; print(sys.stdin.read().strip().lower().capitalize())"
This is my user jenkins

Solution 7 - Linux

Using sed with tr:

name="athens"
echo $name | sed -r "s/^[[:alpha:]]/$(echo ${name:0:1} | tr [[:lower:]] [[:upper:]])/"

Solution 8 - Linux

Using bash (without sed, so a little off topic):

msg="this is a message"
for word in $msg
do
   echo -n ${word^} ""
done

This Is A Message

Solution 9 - Linux

Capitalize the first letter of each word (first lowercase everything, then uppercase the first letter).

echo "tugann sí spreagadh" | sed -E 's/.*/\L&/g' | sed -E 's/(\b.)/\U\1/g'
> Tugann Sí Spreagadh

Solution 10 - Linux

Proposed sed solutions until now will only work if the original text is in lowercase. Although one could use tr '[[:upper:]]' '[[:lower:]]' to normalize the input to lowercase, it may be convenient to have an all-in-one sed solution :

sed 's/\w\+/\L\u&/g'

This will match words (\w means word character, and \+ at least one and until the next non-word character), and lowercase until the end (\L) but uppercase the next (i.e. first) character (\u) on each (g) matched expression (&).

[Credits]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneversaintView Question on Stackoverflow
Solution 1 - LinuxtangensView Answer on Stackoverflow
Solution 2 - Linuxghostdog74View Answer on Stackoverflow
Solution 3 - Linuxrekha_sriView Answer on Stackoverflow
Solution 4 - LinuxmikejoneseyView Answer on Stackoverflow
Solution 5 - Linuxchtimi59View Answer on Stackoverflow
Solution 6 - LinuxHavokView Answer on Stackoverflow
Solution 7 - Linuxmark_infiniteView Answer on Stackoverflow
Solution 8 - LinuxDominique BaldoView Answer on Stackoverflow
Solution 9 - LinuxVilmantas LiubinasView Answer on Stackoverflow
Solution 10 - LinuxSkippy le Grand GourouView Answer on Stackoverflow