微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

How to change any text to Proper Case and Sentence case using tr?

 

According to https://caseconverter.com/

“Upper Case” WHICH CONVERTS ALL THE LETTER INTO CAPITALS LIKE THIS.

“Lower Case” which converts all the letters into small letters like this.

The first two can be accomplish easily with tr command.

user@linux:~$ tr [:lower:] [:upper:] <<< eXaMPLe
EXAMPLE
user@linux:~$ 

user@linux:~$ tr [:upper:] [:lower:] <<< eXaMPLe
example
user@linux:~$ 

or

user@linux:~$ tr [a-z] [A-Z] <<< eXaMPLe
EXAMPLE
user@linux:~$ 

user@linux:~$ tr [A-Z] [a-z] <<< eXaMPLe
example
user@linux:~$ 

 

“Proper Case” Which Converts The Text So Every Word Has Its First Letter Capitalised Like This

“Sentence Case”. This capitalises the first letter of each sentence, and converts the rest of the text to lower case. So the first letter after every full stop is automatically converted into a capital letter.

echo $'ste1phane' | sed -E "s/[[:alnum:]_']+/\u&/g"
Ste1phane

echo $'ste\u0301phane chazelas' | perl -Mopen=locale -pe 's/[\w\pM'\''-]+/\u$&/g'
Stéphane Chazelas

sed 's/\<\([[:lower:]]\)\([[:alnum:]]*\)/\u\1\2/g' file

sed 's/\<\([[:lower:]]\)\([^[:punct:]]*\)/\u\1\2/g' file

 

### Works

echo "this is a test" | sed 's/.*/\L&/; s/[a-z]*/\u&/g'

echo 'This is a TEST' | sed 's/[^ ]\+/\L\u&/g'

 

REF

https://unix.stackexchange.com/questions/554901/how-to-change-any-text-to-proper-case-and-sentence-case-using-tr

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。