Wednesday, 31 July 2019

convert upper case to lower case on Linux, in different ways

convert upper case to lower case on linux, in different ways
There are quite a lot ways to do the task on linux, most of them are easy to use. Here are different useful examples for different cases.

#1 text file editor, vi, vim, ex etc..

They are in the same editor 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:
Command         Explanation
1,$             Line Address location is all i.e. find all lines for following pattern
s               Substitute 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.
g               Global replacement

#2 tr command  -- translate or delete characters

$echo "HOW ARE YOU TODAY" |tr '[:upper:]' '[:lower:]'
how are you today
also works with variable
$echo $VAR_NAME | tr '[:upper:]' '[:lower:]'

Convert whole file

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. Using 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 FIBREVILLAGE"
$ echo ${string1,,}
i love fibrevillage

Typeset

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

#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

Upper case to lower case
$ 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 conver lower case to upper case
$ perl -pe '$_= uc($_)' test1
SINGLE WORDS OR PHRASES THAT DESCRIBE YOURSELF
To conver upper case to lower case
$ perl -pe '$_= lc($_)' test2
single words or phrases that describe yourself

A piece of simple code to convert file to lower or 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

0 comments:

Post a Comment