0

This question already has an answer here:

input

   > a='Vikas'
   > echo $a
    Vikas

my required output is

echo $a | <some command>

vIKAS

marked as duplicate by Stéphane Chazelas text-processing Nov 22 '17 at 13:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Looks like homework to me – Raman Sailopal Nov 22 '17 at 12:49
  • very simple, use tr command.. – Sundeep Nov 22 '17 at 12:51
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.

  • 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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.