tr command in Linux Explained With Examples
Commands

tr command in Linux Explained With Examples

Abhishek Prakash
Abhishek Prakash

The ‘tr’ in tr command stands for translation. This nifty command is used for translating one type of characters into another. For example, if you want to convert text into all upper cases or all lower cases, tr command is what you can use.

The tr command is good enough for simple and quick translation but it doesn’t match the power of awk or sed commands.

Practical examples of the tr command in Linux

The tr command has the following syntax:

tr [options] charset1 [charset2]

These are the useful options for the tr command:

  • -d : Delete the characters in the first set
  • -c : Complement the first set of characters i.e., operate only on the characters NOT in the first set
  • -s : Remove multiple adjacent occurrence of the characters in the first set
  • -t : truncate the first set

Let’s see how to use the tr command and its options in various use cases.

1. Convert lower case to upper case and vice versa

One of the most popular use of tr command is to convert the cases. Let me first use the cat command to display the text in my sample file.

abhishek@linuxhandbook:~$ cat sample.txt
This is my life
and my file

Now if you want to convert the text into all capital letters, here’s what you need to do:

cat sample.txt | tr 'a-z' 'A-Z'

Here’s the output of the above command:

abhishek@linuxhandbook:~$ cat sample.txt | tr ‘a-z’ ‘A-Z’
THIS IS MY LIFE
AND MY FILE

You can also use character classes here:

abhishek@linuxhandbook:~$ cat sample.txt | tr [:lower:] [:upper:]
THIS IS MY LIFE
AND MY FILE

To convert the text into all lower cases, you just have to interchange the character sets:

cat sample.txt | tr 'A-Z' 'a-z'

2. Replacing one set of characters with another

In the above example, you converted the lower case to upper case. The same way, you can replace one set of characters with another set of characters. The matching is done on one by one basis.

Let me explain with this example:

abhishek@linuxhandbook:~$ cat sample.txt | tr ‘ilm’ ‘tyz’
Thts ts zy ytfe
and zy ftye

In this case, the first set of character is replaced with the second set, per character basis. Here ‘i’ is replaced with ‘t’, ‘l’ with ‘y’ and ‘m’ with ‘z’.

If the second set is smaller than the first, last character of the second set is repeated. In other words, if you had used tr ‘ilm’ ‘ty’ then both ‘l’ and ‘m’ would have been replaced with ‘y’.

One practical example of this method would be to replace () with {} or replacing underscore (_) with dash (-) something like that. You can also convert tabs to spaces in Linux the same way.

3. Delete specific character(s)

If you want to delete specific character or characters, you can use the -d option of the tr command:

abhishek@linuxhandbook:~$ cat sample.txt | tr -d ‘is’
Th my lfe
and my fle

In the above example, all the instances of ‘i’ and ‘s’ have been deleted. If you were expecting only ‘is’ to be deleted, then you were wrong. This is why sed command is a far better option when it comes to text manipulation.

4. Remove repetitive characters like multiple spaces

Suppose your text has multiple spaces and you want to replace the multiple spaces with a single space.

I am going to use the echo command for the sample text here:

abhishek@linuxhandbook:~$ echo “Text has too many spaces” | tr -s ” “
Text has too many spaces

You can also replace the multiple occurrences of a character with another character. For example, you can replace all occurrences of spaces with a semicolon (;).

abhishek@linuxhandbook:~$ echo “Text has too many spaces” | tr -s ” ” “;”
Text;has;too;many;spaces

5. Remove all the non-digit characters

Let’s say you have text that contains both letters and numbers and you want to keep only numbers. You can use the complement option -c and combine it with the delete option -d.

abhishek@linuxhandbook:~$ echo “Phone number is 123456789” | tr -cd [:digit:]
123456789

6. Truncate a search pattern

The option -t is useful in truncating a search pattern. Suppose the first set of characters is bigger than the second set of characters. In that case, the last character in the second set replaces all the remaining characters of the first set.

Let me explain this with this example:

abhishek@linuxhandbook:~$ cat sample.txt | tr ‘isef’ ’12’
Th12 12 my l122
and my 21l2

In the above example ‘i’ is replaced with 1 and all of s, e and f are replaced with 2. If you use the truncate option -t, it will truncate the first set of characters at ‘is’ and the remaining e and f will be untouched:

abhishek@linuxhandbook:~$ cat sample.txt | tr -t ‘isef’ ’12’
Th12 12 my l1fe
and my f1le

There could be numerous other examples of the tr command in Unix/Linux. This tutorial explains some examples that utilize the various option of the command. You can use your imagination and use them in a variety of situations.

I hope you found this tutorial helpful. Don’t forget to subscribe to our weekly newsletter and get useful Linux tips in your inbox.



Join the conversation.