Uppercasing First Letter of Words Using SED

前端 未结 9 1357
误落风尘
误落风尘 2020-12-07 20:49

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         


        
9条回答
  •  借酒劲吻你
    2020-12-07 21:47

    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]

提交回复
热议问题