Along with the Linux sed command, the tr command stands for translate is used to provide a level of swapping or translation, suppression or deletion of files. The tr command just translates one character to another character. In this article, we’ll share a couple of examples demonstrating some exciting things that we can do with the tr command.

The basic syntax for the tr command

tr [OPTION] [SET1] [SET2]

SET1 denotes what we wish to translate in the input file, and SET2 means what we want to convert SET1 as the output of the translation. So the sets can be a single character or multiple characters.

Example1: Suppose you just want to replace a in “sahil suri” with b we can use tr sets. We use echo command to send “sahil suri” to tr command. The tr command by default is not able to read data stream. We use Linux inbuilt redirection operators to feed data to tr command.

[root@linuxnix:~]# echo "sahil suri" | tr 'a' 'b'

sbhil suri

Example2: Suppose if you want to replace multiple characters one after the other then we can use below-set examples. Suppose you want to replace a with b, r with x and i with z, below is the case you are looking at.

[root@linuxnix:~]# echo "sahil suri" | tr 'ari' 'bxz'

sbhzl suxz

Note: Make sure that first set and second set have the same number of characters. For example,  i is just replaced with z as we don’t have the third character on the list below.

root@linuxnix:~# echo "sahil suri" | tr 'ari' 'bz'

sbhzl suzz

Similarly, we can use sets to convert multiple letters. Some of the sets tr supports are as below.

POSIX Character set supported by tr command

[:digit:] Only the digits 0 to 9

[:alnum:] Any alphanumeric character

[:alpha:] Any alpha character A to Z or a to z.

[:blank:] Space and TAB characters only.

[:xdigit:] Hexadecimal notation 0-9, A-F, a-f.

[:punct:] Punctuation symbols . , ” ‘ ? ! ; : # $ % & ( ) * + – / < > = @ [ ] \ ^ _ { } | ~

[:print:] Any printable character.

[:space:] Any whitespace characters (space, tab, NL, FF, VT, CR). Many system abbreviates as \s.

[:graph:] Exclude whitespace (SPACE, TAB). Many system abbreviate as \W.
[:upper:] Any alpha character A to Z.
[:lower:] Any alpha character a to z.
[:cntrl:] Control Characters NL CR LF TAB VT FF NUL SOH STX EXT EOT ENQ ACK SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC IS1 IS2 IS3 IS4 DEL.

Character sets supported by tr command

  1. a-z To represent complete lower case alphabets
  2. A-Z to represent upper case alphabets
  3. 0-9 To describe the set of numbers.

Example 3: Convert all characters in input from lower to upper case.

[root@linuxnix ~]# echo "sahil suri" | tr 'a-z' 'A-Z'
SAHIL SURI

The tr command mentioned above implies to translate all alphabets in lowercase denoted by ‘a-z’ into alphabets in upper case denoted by ‘A-Z’

Example 4: Convert all characters in input from upper to lower case.

[root@linuxnix ~]# echo "SAHIL SURI" | tr 'A-Z' 'a-z'
sahil suri
[root@linuxnix ~]#

Example 5: Translate only particular characters from lower to upper case.

[root@linuxnix ~]# echo "sahil suri" | tr 'shlr' 'SHLR'
SaHiL SuRi
[root@linuxnix ~]#

In the previous two examples, we gave ranges, but we can use distinct characters as well as shown in the above example.

Example 6: Use multiple ranges while performing a translation operation.

[root@linuxnix ~]# echo "sahil suri" | tr 'a-dr-z' 'A-DR-Z'
SAhil SURi
[root@linuxnix ~]#

Example 7: We can translate individual characters as well. Translate braces into parenthesis.

[root@linuxnix ~]# echo "{ Hello World }" > infile 
[root@linuxnix ~]# tr '{}' '()' < infile > outfile 
[root@linuxnix ~]# cat outfile 
( Hello World )

Example 8: Translate spaces to new lines. We use infile as our test file. Use cat command to display the content of the file.

[root@linuxnix ~]# cat infile 
solaris linux aix 
[root@linuxnix ~]# cat infile | tr " " "\n" 
solaris 
linux 
aix 
[root@linuxnix ~]# cat infile | tr "[:space:] " "\n"
 solaris 
linux 
aix

While using translate, you may use ” ” or [:space:] to represent white space. I find the first option easier to type.

Example 9: Replace white space with a different delimiter.

[root@linuxnix ~]# cat infile | tr " " ":"
solaris:linux:aix
[root@linuxnix ~]# cat infile | tr " " "|"
solaris|linux|aix

Example 10: Replace multiple occurrences of a character with a single appearance of the character. In below example, we replaced numerous spaces with separate space.

[root@linuxnix ~]# cat infile
solaris      linux   aix
[root@linuxnix ~]#
[root@linuxnix ~]# tr -s " " < infile
solaris linux aix

In the above case, we used the tr command with -s option to indicate a squeeze operation.
This replaces multiple occurrences of a character with a single appearance of the same character.

Example 11: Replace multiple occurrences of a character with a different character.

[root@linuxnix ~]# cat infile
solaris linux aix
[root@linuxnix ~]# tr -s " " ";" < infile
solaris;linux;aix

The above tr command squeezes multiple occurrences of white space into one semicolon character.

Example 12: Delete all occurances of a character.

To delete occurrences of a character we use the -d option with the tr command followed by the character to be removed.

[root@linuxnix ~]# echo "linuxunix" | tr -d 'u'
linxnix

The above command removes all occurrences of the character u from the input string.

Example 13: Remove all digits from the input.

[root@linuxnix ~]# echo "james bond 007" | tr -d '0-9'
james bond

Example 14: Remove all alphabets from the input.

[root@linuxnix ~]# echo "james bond 007" | tr -d 'a-zA-Z'
007

Example 15: Remove all characters except the given set with tr.

We used -d option to delete characters we mentioned from the input.
We may use the -c option with the -d option to delete all characters except those that we mention.

[root@linuxnix ~]# echo "solaris linux hp-ux" | tr -cd 'slh'
slslh[root@linuxnix ~]#

The above tr command removed all characters including the new line leaving behind the characters s,l and h which we mentioned to be removed.
Using the -c option is also sometimes referred to as complimenting the set.

Example 16: Remove all non-alphanumeric and space characters.

[root@linuxnix ~]# echo "solaris | linux 007 ; hp-ux :::: aix" | tr -cd "[:alpha:][:space:][:digit:]"
solaris linux 007 hpux aix
[root@linuxnix ~]#

This example could prove to be especially useful when dealing with a file having a lot of junk characters.

Example 17: Translate non-alphanumeric characters into a single newline character.

[root@linuxnix ~]# echo "james bond 007" | tr -cs 'a-zA-Z0-9' '\n'
james
bond
007

Example 18: Join all lines in a file into a single line.

[root@linuxnix ~]# cat infile
solaris
linux
aix
[root@linuxnix ~]#
[root@linuxnix ~]# tr -s '\n' ' ' < infile
solaris linux aix [root@linuxnix ~]#

The above tr command squeezes the new line character into a single space character.
This option can be useful when attempting to consolidate scattered text in a file.

This concludes our exploration of the tr command.

Please consider reading our articles about sed which is another utility that provides most of the translation features we discussed in the above examples and a lot more.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.