tr Translate Command

The "tr" command (translate) is used to literally translate or delete characters. The "tr" command normally takes two sets of characters and then replaces these with the corresponding characters found within the second set.

Syntax: tr [OPTIONS] SET1 SET2

tr command examples

In the following example we use the "tr" command to translate text that is keyed in. Any letter entered in lowercase will be translated to upper case.


$ tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
welcome to the land of linux
WELCOME TO THE LAND OF LINUX

An easier way to translate between "upper" and "lower" case characters is to use the "[:lower:]" and "[:upper:]" options.


$ echo 'welcome to the land of linux' | tr "[:lower:]" "[:upper:]"
WELCOME TO THE LAND OF LINUX

A popular method for achieving the same result can be achieved by specify a range of characters: "a-z" and "A-Z". In the following example the text "welcome to the land of linux" will be translated to upper case characters.


$ echo 'welcome to the land of linux' | tr "a-z" "A-Z"
WELCOME TO THE LAND OF LINUX

Translating the contents of a file

In the following example we have used a file called "test.txt". The contents of the file are displayed below.


$ cat test.txt
start[]
middle[]
end[]

Here we read in the contents of our text file using redirection, then we write the translated version of the file to a new file called "newfile.txt".


$ tr '[]' '()' < test.txt > newfile.txt

$ cat newfile.txt
start()
middle()
end()

Deleting Specified Characters

The "-d" parameter can be used to delete characters.


$ echo "This is another test" | tr -d 'a'
This is nother test

In the above example the character "a" was removed.

Deleting New Line Characters

The following file will be used as an example.


$ cat test2.txt
Line one
Line two
Line three
Line four
Line Five

The "-d" option is used to specify the "tr" command to delete a character. A common use for the "tr" command is to remove "new line" characters from a file. The new line character is specified as "\n".


$ cat test2.txt | tr -d "\n"
Line oneLine twoLine threeLine fourLine Five

Interpreted Sequences

As well as the "-n" option. You can also specify any of the following values:


       \NNN   character with octal value NNN (1 to 3 octal digits)

       \\     backslash

       \a     audible BEL

       \b     backspace

       \f     form feed

       \n     new line

       \r     return

       \t     horizontal tab

       \v     vertical tab

Translate new lines to a single space


$ tr -s '\n' ' ' < test2.txt
Line one Line two Line three Line four Line Five 

White Space to Tabs


$ echo "This is a test" | tr [:space:] '\t'
This    is      a       test

White Space to NewLine


$ echo "This is a test" | tr [:space:] '\n'
This
is
a
test

Removing Spaces


$ echo "This    is     for     testing" | tr -s [:space:] ' '
This is for testing

tr Options


       -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

       -t, --truncate-set1
              first truncate SET1 to length of SET2

       --help display this help and exit