0

input

   > a='Vikas'
   > echo $a
    Vikas

my required output is

echo $a | <some command>

vIKAS
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

2 Answers2

5
$ echo Vikas | LC_ALL=C tr a-zA-Z A-Za-z
vIKAS

The utility tr translates characters; it takes two arguments representing sets of characters; it then copies standard input to standard output replacing each character found in the first set with the corresponding character in the second set. In this application, it replaces lowercase letters with uppercase letters and vice-versa. See the manual page of tr(1) for details and for other processing which tr can perform.

AlexP
  • 10,455
  • what is LC_ALL=C @AlexP, without that its also giving same output – Sanket Nov 22 '17 at 13:01
  • thanks Alex.

    echo Vikas | tr [a-z][A-Z] [A-Z][a-z]

    vIKAS

    can you please say why LC_ALL=C is required ?

    – Vikas Venna Nov 22 '17 at 13:03
  • Without setting the locale to C, the meaning of a-z and A-Z is unknown, since these ranges are locale dependent (for example, in some locales this will also include non-ASCII characters). Setting it to C guarantees that these ranges mean what most people expect. – Chris Down Nov 22 '17 at 13:06
  • @VikasVenna: Many utilities interpret a range such as A-Z as representing all characters which sort between A and Z in the current locale. Your tr may do this or not, I don't know. By setting LC_ALL=C I am certain that A-Z means ABCDEFGHIJKLMNOPQRSTUVWXYZ. – AlexP Nov 22 '17 at 13:11
3

you can use tr command with [:upper:] and [:lower:] options, like this:

echo "aBcDeF" |tr '[:upper:][:lower:]' '[:lower:][:upper:]'
AbCdEf

also you can use sed command (stands for 'Stream editor'), like this:

echo "abcd ABCD" |sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/'

sed has more flexibility, it means you can define any character mapping. for example you can convert numbers to hackers letter with this command:

echo "52065218802365" |sed 'y/0123456789/OIZEhSGLBP/'
SZOGSZIBBOZEGS

(character mapping reference)

Giac
  • 131