Sunday, 10 January 2016

8 Practical Examples Of TR Command In Unix/Linux

It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set. The utility reads a byte stream from its standard input and writes the result to the standard output.As arguments, it takes two sets of characters (generally of the same length), and replaces occurrences of the characters in the first set with the corresponding elements from the second set. We will see it in details.

SYTAX:

$ man tr
TR(1)                                                               

NAME

       tr - translate or delete characters
SYNOPSIS
       tr [OPTION]... SET1 [SET2]
DESCRIPTION
       Translate, squeeze, and/or delete characters from standard          input, writing to standard output.
       -c, -C, --complement
              use the complement of SET1
       -d, --delete
              delete characters in SET1, do not translate
       -s, --squeeze-repeats
              replace each input sequence of a repeated character                 that is listed in SET1 with a single occurrence of                   that character

What is this ? no worries, we will see all with the help of examples.

1. Replace the characters

$ echo "Unix Systems are Unique" | tr "U" "L"
Output: tr command has replaced U with L because it has matched with the criteria.
Lnix Systems are Lnique

2. Replace all the characters other than matched

$ echo "Unix Systems are Unique" | tr -c "U" "*"
Output: tr command has replaced everything with * except U because it has matched with the criteria.
U****************U******

3. Delete all the matching characters

$ echo "Unix Systems are Unique" | tr -d "U" 

Output: tr has deleted U from the input data.
nix Systems are nique

4. Delete all the non-matching character

$ echo "Unix Systems are Unique" | tr -cd "U"
Output: tr has delete everything which has not matched with "U". This is because of -c switch.
UU

5. How to replace repetitive occurrence of a pattern with single occurrence

$ echo "Unix@@@System" | tr -s "@"
Output:
Unix@System

$ echo "Unix@@@System" | tr -s "@" " "
Output:
Unix System

You might be thinking that we can do the same without using any switch, let see one example without -s switch

$ echo "Unix@@@System" | tr "@" "#"
Output:
Unix###System


Observe the output, here tr has replaced each occurrence of @ with # that is not our requirement. Our requirement is to replace the repetitive character with single occurrence which can be done by using -s switch.

6. Lower case to upper case

$ echo "Uinx is Awesome" | tr "[:lower:]" "[:upper:]"
Output: tr has used the class notation and converted lower case input data into upper case.
UINX IS AWESOME

We can write the same as below using range expression.

$ echo "Uinx is Awesome" | tr "[a-z]" "[A-Z]"

7. Upper case to lower case

$ echo "UINX IS AWESOME" | tr "[:upper:]" "[:lower:]"
Output: tr has used the class notation and converted upper case input data into lower case.
uinx is awesome 

We can write the same as below using range expression.

$ echo "Uinx is Awesome" | tr "[A-Z]" "[a-z]"

8. How to replace the text in a file

$ tr "unix" "Unix" <F_Input_File.txt

[OR]

$ tr "unix" "Unix" <F_Input_File.txt > F_Output_File.txt

[OR]

$ cat F_Input_File.txt | tr "unix" "Unix" > F_Output_File.txt 

Output: It will replace every unix with Unix in the file F_Input_File.txt


Conclusion: tr command is very hand to manipulate the data. It can be used to validate the data by converting it in lower or upper. We can remove unnecessary repetitive characters and make our data more authentic.

Keep Reading, Keep Learning,Keep Sharing...!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...