The “tr” Command in Linux [6 Practical Examples]

The tr command in Linux is a command-line utility that comes pre-installed with most distributions of the Linux operating system and helps manipulate and replace or delete characters in text files. Here, in this article, I will describe all of the characteristics and applications of the tr command in Linux with numerous examples.

A. Description

The tr command in Linux is a utility used to replace or delete characters from standard input and write the results to standard output. It is a simple and effective tool for text manipulation, particularly for cleaning up or transforming text files. With its basic syntax, the tr command can perform a wide range of operations.

B. Syntax

The syntax of the tr command in Linux is pretty simple, as shown below.

tr [OPTION]... SET1 [SET2]

Note: In the syntax above, the OPTION and SET2 enclosed by the square brackets mean they are not mandatory and three dots after the square brackets mean multiple OPTIONs can be used after the tr command.

C. Options

There are numerous options available for the tr command. Here, I have listed a few of them. However, you can learn more about the tr command, its options and their uses by checking the man page.

man tr

Useful Options

  • -c, –complement: Uses the complement of SET1.
  • -d, –delete: Deletes the characters specified in the SET1 argument.
  • -s, –squeeze-repeats: Replaces each sequence of repeated characters in SET1 with a single instance of that character.
  • -t, –truncate-set1: Truncates SET1 to length of SET2.
  • –help: Displays the help instruction.
  • –version: Displays version information.

Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.

Practical Examples of the “tr” Command in Linux

In Linux, the tr command is a helpful tool to replace or delete characters from input and write the result to output. In the section below, I will show you some of the most useful applications for the tr command in Linux.

Download this file to work with the “tr” command in Linux.

Example 1: Change Case of Characters Using the “tr” Command in Linux

The tr command in Linux allows you to change the case of characters. You can change the case of each character or only one character by running the tr command. Here, I will show you an example of changing the case of each character. You can do the same by following the steps given below.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

➋ Then, execute the below command to change the case of each character.

echo "hello world" | tr [a-z] [A-Z]

Alternatively,

echo "hello world" | tr [:lower:] [:upper:]

➌ Finally, tap the ENTER key again.

Output:

In the following image, you can see that I have changed the case of each character in the standard output from the echo command.Showing that I have changed the case of each character in the standard output from the echo command.


Similar Readings


Example 2: Remove Repeated Characters Using the “tr” Command in Linux

You can remove repeated characters using the tr command with the option -s. In this example, I will show you the process to remove the character “e” from a line where “e” is repeated more than one time. You can use this process whenever you need it by simply following the steps mentioned below.

Steps to Follow:

➊ First, open the Ubuntu Terminal.

➋ Next, run the following command to remove the repeated characters.

echo "Welcomeeeee to this exampleeeeeeee" | tr -s "e"

➌ Lastly, tap the ENTER button.

Output:

In the image below, you can see that I have removed the repeated character “e” from a string.The repeated character “e” has been removed from a string using the tr command in Linux.

Example 3: Delete a Specific Character Using the “tr” Command in Linux

The tr command in Linux allows you to delete any character from any line. Here, I will show you an example of deleting the character “W” from the word “Welcome”. You can do the same by following the steps below.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

➋ Then, execute the given command.

echo "Welcome to this example" | tr -d "W"

➌ Finally, tap the ENTER key again.

Output:

In the following image, you can see that I have deleted the character “W” from the word “Welcome”.The character “W” has been removed from the word “Welcome” using the tr command in Linux.


Similar Readings


Example 4: Remove All Non-numeric Characters

You can remove all non-numeric characters by using the tr command with the option -cd. In this example, I will show you the process to remove all the non-numeric characters from a string. You can use this process whenever you need it by simply following the steps mentioned below.

Steps to Follow:

➊ First, open the Ubuntu Terminal.

➋ Next, run the command to remove the non-numeric characters.

echo "My house number is 7852" | tr -cd [:digit:]'\n'

➌ Lastly, tap the ENTER button.

Output:

In the image below, you can see that I have removed all the non-numeric characters from a string.All the non-numeric characters have been removed from a string using the tr command in Linux

Example 5:  Remove All the Digits from a String Using the “tr” Command in Linux

The tr command in Linux allows you to remove all digits from a string. Here, I will show you an example of removing all digits from the string “My house number is 7852”. You can do the same by following the steps given below.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

➋ Then, execute the following command, to remove all digits.

echo "My house number is 7852" | tr -d [:digit:]

➌ Finally, tap the ENTER key again.

Output:

In the following image, you can see that I have removed all digits from the string.Showing that I have removed all digits from a string.


Similar Readings


Example 6: Remove Newline Characters Using the “tr” Command in Linux

You can remove all newline characters (\n) by using the tr command with the option -s. In this example, I will show you the process to remove all newline characters from a string. You can use this process when you need it by simply following the steps given below.

Steps to Follow:

➊ First, open the Ubuntu Terminal.

➋ Next, run the below command to see the contents of the myinfo.txt file.

cat myinfo.txt

➌ Now, Press the ENTER button.

➍ At this point, execute the following command.

cat myinfo.txt | tr -s '\n' ' '

➎ Lastly, tap the ENTER button.

Output:

In the image below, you can see that I have removed all the newline characters from a string.Showing that I have removed all the newline characters from a string.

Conclusion

The tr command in Linux provides a simple and effective way to manipulate text. It is important to note that it only operates on single lines of text at a time. In this article, I’ve discussed the tr command and explained its options with some relevant examples. With this article, you should now have a better understanding of the tr command and how to use it. Hopefully, this article will make it easier for you to become skilled at using the Linux command line.


Similar Readings

Rate this post
S. M. Amdadul Islam

S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

We will be happy to hear your thoughts

Leave a reply

LinuxSimply
Logo