Convert all text from uppercase to lowercase and vice versa?

My question is how can I convert all text from uppercase to lowercase and vice versa? That is to change the cases of all the letters. It has to be done with a sed replacement somehow.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here is a straight way in sed:

$ echo qWeRtY | sed -e 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/'
QwErTy

or a shorter way with GNU sed, working with any character for which a lowercase<->uppercase conversion exists in your locale:

$ echo qWeRtY | sed -E 's/([[:lower:]])|([[:upper:]])/U1L2/g'
QwErTy

if you can use another tools, like:

perl (limited to ASCII letters):

$ echo qWeRtY | perl -pe 'y/[a-z][A-Z]/[A-Z][a-z]/'
QwErTy

perl (more generally):

$ echo 'αΒγ' | perl -Mopen=locale -pe 's/(p{Ll})|(p{Lu})/uc($1).lc($2)/ge'
ΑβΓ

Method 2

POSIXly, that can’t be done with sed except by providing the complete set of letters you want to transliterate as @cuonglm has shown.

It could be done with tr though, and that’s what tr is for (transliterate):

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

However, on Linux, it’s got limitations. Of the 3 tr implementations commonly found on Linux-based systems:

  • with GNU tr, that only works for single-byte character sets. For instance, on Stéphane Chazelas in UTF-8 locales, that gives sTéPHANE cHAZELAS instead of sTÉPHANE cHAZELAS. That’s a known limitation of GNU tr.
  • with tr from the heirloom toolchest, that doesn’t work (you get stéphane chazelas).
  • That’s not the kind of thing busybox tr will do.

On FreeBSD that works OK though. You’d expect it to work OK in certified Unix systems as well.


The bash shell has a dedicated operator for that:

in=AbCdE
out=${in~~}

With zsh -o extendedglob:

out=${in//(#b)(([[:lower:]])|([[:upper:]]))/${(U)match[2]}${(L)match[3]}}

Method 3

If your main objective is to convert a file from lowerclass to upperclass,
why dont you use tr and STDOUT to convert your file:

$cat FILENAME | tr a-z A-Z > FILENAME2

Where FILENAME is your original file.
Where FILENAME2 is your converted output file.

Method 4

Though this has the same limitations already mentioned as the tr solution offered by Stéphane Chazelas, it is another way to do it:

{   echo QWERTYqwerty | dd conv=lcase
    echo QWERTYqwerty | dd conv=ucase 
} 2>/dev/null

OUTPUT

qwertyqwerty
QWERTYQWERTY

I dump stderr into /dev/null there because dd also provides statistics of all its operations on the 2 file descriptor. This can be useful depending on what you’re doing, but wasn’t for this demonstration. All of the other stuff you can do with dd still applies, for instance:

echo QWERTYqwerty | dd bs=1 cbs=6 conv=unblock,ucase 2>/dev/null

OUTPUT:

QWERTY
QWERTY

Method 5

ruby has a string method for that, similar usage from command line like perl

$ echo 'qWeRtY' | ruby -pe '$_.swapcase!'
QwErTy

See also ruby-doc Encoding

$ ruby -e 'puts Encoding.default_external'
UTF-8
$ echo 'αΒγ'  | ruby -pe '$_.swapcase!'
ΑβΓ

Method 6

using awk:

awk '{print tolower($0)}' file.txt | tee file.txt

Method 7

Keep simple thing simple. The filter designed to translate characters is tr.

echo 1ude1UDE | tr [:upper:][:lower:] [:lower:][:upper:]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x