Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize first letter of every word using sed in OSX

I'm trying to capitalize the first letter of every word in a string using the following sed command, but it's not working:

 echo "my string" | sed 's/\b\(.\)/\u\1/g'

Output:

 my string

What am I doing wrong?

Thank you

like image 725
Ares Avatar asked Oct 21 '25 06:10

Ares


1 Answers

Given your sample input, this will work in any awk:

$ echo 'my string' | awk '{for (i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2)} 1'
My String

If that doesn't do what you really want then edit your question to show some more truly representative sample input and expected output.

like image 98
Ed Morton Avatar answered Oct 23 '25 20:10

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!