Linux sed script to make the first letter of each word in uppercase

I am trying to make a sed script that will make the first letter of each

molly w. bolt     334-78-5443
walter q. bugg    984-49-0032
noah p. way       887-12-0921

So, I want it to look like this:

molly w. bolt     334-78-5443
walter q. bugg    984-49-0032
noah p. way       887-12-0921

So far I have the following script, but it will use only the first two words that it meets, i.e. making him Molly V. a bolt. I cannot figure out how to change the script to get the name in uppercase. What do I need to add?

h
s/\(.\).*/\1/
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
G
s/\(.\)\n\(.\)\(.*\)/\1\3/
/ [a-z]/{
    h
    s/\([A-Z][a-z]* \)\([a-z]\).*/\2/
    y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
    G
    s/\(.\)\n\([A-Z][a-z]* \)\(.\)\(.*\)/\2\1\4/
}
+4
source share
2 answers

this gnu sed single liner can help you:

sed 's/\b./\u&/g' file

check your details:

kent$  cat f
molly w. bolt 334-78-5443
walter q. bugg 984-49-0032
noah p. way 887-12-0921

kent$  sed 's/\b./\u&/g' f
Molly W. Bolt 334-78-5443
Walter Q. Bugg 984-49-0032
Noah P. Way 887-12-0921
+5
source

Use uppercase \band \Uuppercase:

sed 's/\b./\U&/g'
+4
source

Source: https://habr.com/ru/post/1609045/


All Articles