Translate Command

Howto guide for using the "tr" command

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:



john@ls001a:~> 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:



john@ls001a:~> 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:



john@ls001a:~> 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:



john@ls001a:~> 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":



john@ls001a:~> tr '[]' '()' < test.txt > newfile.txt

john@ls001a:~> cat newfile.txt
start()
middle()
end()


Deleting Specified Characters


The "-d" parameter can be used to delete characters with ease:



john@ls001a:~> 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:



john@ls001a:~> 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".



john@ls001a:~> 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



john@ls001a:~> tr -s '\n' ' ' < test2.txt
Line one Line two Line three Line four Line Five 

White Space to Tabs



john@ls001a:~> echo "This is a test" | tr [:space:] '\t'
This    is      a       test

White Space to NewLine



john@ls001a:~> echo "This is a test" | tr [:space:] '\n'
This
is
a
test

Removing Spaces



john@ls001a:~> 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