Unix tr command to convert lower case to upper AND upper to lower case
Asked Answered
D

3

18

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 what I want is "hELLO wORLD abc".

Dissociate answered 20/4, 2014 at 5:25 Comment(0)
K
22

This will do what you are looking for:

 tr '[:upper:][:lower:]' '[:lower:][:upper:]'
Kashmiri answered 20/4, 2014 at 5:29 Comment(1)
This is technically correct, ... I feel it should be noted (to less discerning readers) that the question is about reversing the casing of a string, not about how to simply uppercase/lowercase a string with tr. tr requires two arguments; echo "FOO" | tr '[:upper:][:lower:]' will not work.Pepita
P
5

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

Pat answered 13/12, 2016 at 7:10 Comment(1)
tr a-zA-Z A-Za-zParticia
H
2

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

Hemidemisemiquaver answered 29/3, 2017 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.