Scripting‎ > ‎Linux shell‎ > ‎

convert upper case to lower case in linux, in different ways

Thank you for visiting this page, this page has been update in another link Convert upper case to lower case in linux in different ways
There are quite a lot ways to do the task in linux, most of them are easy to use, but good for different use case.

#1 text file editor, vi, vim, ex etc.. they are in the same family

Open the file you want to conver all upper case to lower case, then use the following commands
:1,$ s/[a-z]/\u &/g

Above command can be explained as follows:

CommandExplanation
1,$ Line Address location is all i.e. find all lines for following pattern
sSubstitute command
/[a-z]/Find all lowercase letter - Target
\u&/Substitute to Uppercase. \u& means substitute last patter (&) matched with its UPPERCASE replacement (\u) Note: Use \l (small L) for lowercase character.
gGlobal replacement

#2 tr command  -- translate or delete characters

$echo "HOW ARE YOU TODAY" |tr '[:upper:]' '[:lower:]'
how are you today

also works for variable
$echo $VAR_NAME | tr '[:upper:]' '[:lower:]'

Another way to convert file test1 content to lower case
$cat test1; tr '[:upper:]' '[:lower:]' <test1 >test2
SINGLE WORDS OR PHRASES THAT DESCRIBE YOURSELF
$cat test2
single words or phrases that describe yourself

This style works the same way
tr '[A-Z]' '[a-z]'

All of the character classes are here:
http://ss64.com/bash/tr.html

#3. Bash

convert lower case to upper case
$ string1="i love itmyshare"

$ echo ${string1^^}
I LOVE ITMYSHARE

convert upper case to lower case
$ string1="I LOVE ITMYSHARE"
$ echo ${string1,,}
i love itmyshare

Another way.

$typeset -u string1="i love itymyshare"
$echo $string1
I LOVE ITMYSHARE

In same way, option '-l' do the opposite way
$typeset -u string1="I LOVE ITMYSHARE"
$echo $string1
i love itmyshare

#4. dd

$ cat test1
SINGLE WORDS OR PHRASES THAT DESCRIBE YOURSELF

$ dd if=test1 of=test2 conv=lcase

$cat test2
single words or phrases that describe yourself

From lower case to upper case, use
dd in=test2 of=test1 conv=ucase

#5. awk

$ awk '{ print tolower($0) }' test1 >test2
From lower case to upper ase, use

$ awk '{ print toupper($0) }' test1 >test2

#6. sed

$ sed -e 's/\(.*\)/\L\1/' test1 >test2
$ cat test2
single words or phrases that describe yourself

The backreference \1 to refer to the entire line and the \L to convert to lower case.

From lower case to upper case
$ sed -e 's/\(.*\)/\U\1/' test2      
SINGLE WORDS OR PHRASES THAT DESCRIBE YOURSELF

#7. perl

To upper case
$ perl -pe '$_= uc($_)' test1

SINGLE WORDS OR PHRASES THAT DESCRIBE YOURSELF

To lower case
$ perl -pe '$_= lc($_)' test2
single words or phrases that describe yourself

A piece of simple code to conver file to lower case or to upper case

        echo "Menu "
        echo "1. Lower to Upper"
        echo "2. Upper to lower "
        echo "3. Quit"
        echo "Enter ur Choice \c"
        read Choice
        case"$Choice"in
           1) echo "Enter File: \c"
              read f1
              if [ -f $f1 ]
          then
               echo "Converting Lower Case to Upper Case "
                 tr '[a-z]''[A-Z]' <$f1
                             
              else
                     echo "$f1 does not exist"
              fi
              ;;
          2) echo "Enter the File :\c"
             read f1
             if [ -f $f1 ]
             then      
               echo "Converting Upper case to Lower Case to "
             tr '[A-Z]''[a-z]' <$f1
                          
             else 
                  echo "$f1 file does not exist "
             fi
             ;;
         3|*) 
             echo "Exit......."
             exit;;
        esac



Comments