Friday, April 24, 2009

Translate or replace characters using tr utility

With tr command you can translate letter from uppercase to lowercase and vice-versa. In other word, with tr command you can replace a letter by another letter.

The syntax for using tr command is,
tr {source_pattern} {destination_pattern}

Each letter in source_pattern is replaced by corresponding letter in destination_pattern. For example if you write

tr "a6" "7y"

then from the string or file every "a" will be replaced by "7",
and every "6" will be replaced by "y".

Let's see an example. My names.txt looks like below.
# cat names.txt
momin
arju
bony
tany
azmeri
queen
tasreen

Now we want letter "o" will be replaced by number "0".
Letter "i" will be replaced by number "1".
Small letter "e" will be replaced by capital letter "E".

# tr "oie" "01E"
m0m1n
arju
b0ny
tany
azmEr1
quEEn
tasrEEn

We can also capitalize all letters inside names.txt with single statement. We can convert to both lowercase to uppercase and vice-versa.

In the following example, letters within names.txt is converted to all capitals.

# tr "a-z" "A-Z" <> names_capital.txt

# cat names_capital.txt
MOMIN
ARJU
BONY
TANY
AZMERI
QUEEN
TASREEN

Related Documents
http://arjudba.blogspot.com/2009/04/join-utility-in-linux.html

No comments:

Post a Comment