We have moved to www.dataGenX.net, Keep Learning with us.

Friday, January 06, 2012

tr command & tips

Hi folks, Here I am with some nice nix commands tips/tricks
For this session, I have "tr" command......

tr command
The tr utility translates one set of characters into another. This utility copies the given input to produced the output with substitution or deletion of selected characters. tr abbreviated as translate or transliterate.

The tr command performs three kinds of operations
a) Transforming Characters
b) Deleting Characters Using the -d Flag
c) Removing Sequences Using the -s Flag


a) Transforming Characters
$echo "She is miss universe"  | tr '[a-z]' '[A-Z]'
SHE IS MISS UNIVERSE


$echo "She is miss universe"  | tr 'es' '#@'
Sh# i@ mi@@ univ#r@#

Explanation :: the tr command replaces each character contained in String1 from the standard input with the character in the same position in String2.


b) Deleting Characters Using the -d Flag


echo "Shhee iiis misss universeee" | tr -d 's'
Shhee iii mi univereee


Explanation :: the tr command deletes each character contained in String1 from standard input.



c) Removing Sequences Using the -s Flag 

$echo "Shhee iiis misss universeee" | tr -s 's'
Shhee iiis mis universeee


$echo "Shhee iiis misss universeee" | tr -s 'si'
Shhee is mis universeee


Explanation :: the tr command removes all but the first character in any sequence of a character string represented in String1



No comments :

Post a Comment