Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux sed script to make first letter of each word uppercase

Tags:

linux

sed

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 only capitalize the first two words it comes across ie. making it Molly W. bolt. I can't figure out how to modify the script to get the last name 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/
}
like image 358
Joe Avatar asked Jan 07 '23 12:01

Joe


2 Answers

this gnu sed one liner may help you:

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

test with your data:

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
like image 140
Kent Avatar answered Jan 16 '23 22:01

Kent


Use the word boundary \b and \U to uppercase:

sed 's/\b./\U&/g'
like image 30
choroba Avatar answered Jan 16 '23 20:01

choroba