Introducing ‘tr’. Lets translate.

Spent  2 hours to write a document or a program but wrote a ‘name’ or a ‘keyword’ wrong every time you used it. What to do now? It must be your ‘karma’ hitting back at you ,wanting you to go through whole text again and replace those words. But you have a life, and here ‘tr’ command becomes the life-saver.

‘tr’ as the name suggests is for translation of some set of characters into some other set of characters.The general syntax of tr is:
                                       tr [options] set1 [set2]

The items in the square brackets are optional. tr requires at least one argument and accepts a maximum of two.
Using without any options, tr will replace the characters provided in set1 with those provided in set2. Thus, for example, every instance of the letter a in text typed in at the keyboard can be replaced with the letter b by entering the following command, press ENTER,type the text, and then again press the ENTER key:
                                   tr a b
We can also input our file with input redirection operator.
                                  tr a b < file1

Multiple characters can also be translated by listing the characters that are to be changed in the first argument and and listing their respective replacements in order in the second argument.
                                tr ‘[efg]’ ‘[xyz]’ < file1

tr can also replace characters in a specified range by their counterparts in another specified range.
                              tr ‘[A-Z]’ ‘[a-z]’ < file8
This would replace uppercase letters with lowercase letters.

                                                        Using options of tr
-s :  replaces each instance of repeated characters in set1 with character in set2
                         tr -s a c < file1
Replaces ‘a’ by ‘c’ wherever ‘a’ is repeated.
-d :  deletes every instance of ‘string’ specified
                         tr -d ‘what’ < file1
Deletes all occurences of ‘what’.

Its almost like sed command in unix but much easier to use. Go translate  :).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s