Unix tr command to convert lower case to upper AND upper to lower case

血红的双手。 提交于 2020-01-23 08:25:21

问题


So I was searching around and using the command tr you can convert from lower case to upper case and vice versa. But is there a way to do this both at once?

So:

$ tr '[:upper:]' '[:lower:]' or $ tr A-Z a-z

will turn: "Hello World ABC" to "hello world abc"

but I want it to do: "hELLO wORLD abc"

Please help! =)


回答1:


This will do what you are looking for:

 tr '[:upper:][:lower:]' '[:lower:][:upper:]'



回答2:


I think tr '[a-zA-Z]' '[A-Za-z]' is more straight forward, and easier to remember.




回答3:


You can use \L& and \U& to convert to lower and upper case respectively:

$echo "SUJIT dhamale " | sed 's/.*/\L&/g'

Result: sujit dhamale

$ echo "SUJIT dhamale " | sed 's/.*/\U&/g'

Result: SUJIT DHAMALE



来源:https://stackoverflow.com/questions/23178769/unix-tr-command-to-convert-lower-case-to-upper-and-upper-to-lower-case

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!