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

 

posted @ 2020-07-30 16:21  emanlee  阅读(182)  评论(0编辑  收藏  举报