Wednesday, February 24, 2016

Convert all text in a file from UPPER to lowercase on LINUX

Convert all text in a file from UPPER to lowercase

To translate or delete characters use tr command. The basic syntax is:

Lets create a simple text file with mix of lower and upper case chars :
oracle@Linux01:[/u01/app/oracle/admin/bin] $ vi TGTGTG.txt

oracle@Linux01:[/u01/app/oracle/admin/bin] $ cat TGTGTG.txt
as This IS in Upper

Here I am passing the TGTGTG.txt as inprt and converting the output to lower case:
oracle@Linux01:[/u01/app/oracle/admin/bin] $ tr '[:upper:]' '[:lower:]' < TGTGTG.txt > output.txt

oracle@Linux01:[/u01/app/oracle/admin/bin] $ cat output.txt
as this is in upper

Here I am passing the TGTGTG.txt as inprt and converting the output to UPPER case:
oracle@Linux01:[/u01/app/oracle/admin/bin] $ tr '[:lower:]' '[:upper:]' < TGTGTG.txt > output_UP.txt

oracle@Linux01:[/u01/app/oracle/admin/bin] $ cat output_UP.txt
AS THIS IS IN UPPER

oracle@Linux01:[/u01/app/oracle/admin/bin] $

EXIT status messages in NIX

Exit Status

By default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not.

(1) If return value is zero (0), command is successful.
(2) If return value is nonzero, command is not successful or some sort of error executing command/shell script.

This value is know as Exit Status.

But how to find out exit status of command or shell script?
Simple, to determine this exit Status you can use $? special variable of shell.

For e.g. If I want to remove a non-existing file

$ rm ABCD
rm: cannot remove `ABCD': No such file or directory
and after that if you give command
$ echo $?
it will print nonzero value to indicate error. Now give command

$ ls
$ echo $?
It will print 0 to indicate command is successful.

Exercise
Try these exit status and check for yourself:

$ expr 1 + 3
$ echo $?

$ echo Welcome
$ echo $?

$ wildwest canwork?
$ echo $?

$ date
$ echo $?

$ echon $?
$ echo $?

Tuesday, February 16, 2016

Uppercase to lowercase or vice versa in BASH

Uppercase to lowercase or vice versa

The bash version 4.x+ got some interesting new features. Type the following commands to convert $VAR into uppercase:


VAR="All THIS will be in UppER Case"
echo "${VAR^^}"

Sample outputs:

ALL THIS WILL BE IN UPPER CASE

Type the following commands to convert $VAR into lowercase:


VAR="All THIS will be in lOWer Case"
echo "${VAR,,}"

Sample outputs:

all this will be in lower case