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. It is 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.

Here, in this article, I will describe all of the characteristics and applications of the tr command in Linux with 6 practical examples.

Linux “tr” Command 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.

Linux “tr” Command 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

 

Options

Description

-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 a length of SET2.
– -help Displays the help instruction.
– -version Displays version information

Note: Commands and their options are case-sensitive in Linux.

6 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 6 of the most useful applications for the tr command in Linux.

Example 1: Change Case of Characters

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:

  1. At first, open the Ubuntu Terminal.
  2. Then, type the below command and tap the ENTER key to change the case of each character.
    echo "hello world" | tr [a-z] [A-Z]

    Alternatively,

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

    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

You can remove repeated characters using the tr command with the option -s. For example, to remove the character “e” from a line where “e” is repeated more than one time, Run the following command:

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

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

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 running the command below:

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

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. Similarly, you can use this process by executing the command mentioned below:

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

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

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” by running the following command:

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

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

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:

  1. Type the below command and press ENTER to see the contents of the myinfo.txt file:
    cat myinfo.txt
  2. Then, execute the following command:
    cat myinfo.txt | tr -s '\n' ' '

    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.

People Also Ask

Can I print each word of a sentence separately using the “tr” command in Linux?

Yes, you can. For instance, to print the words of the sentence “ welcome to linuxsimply” in separate lines, run the following command with tr command and -cs options:

echo "welcome to linuxsimply" | tr -cs [:alnum:] '\n'

Output:

Welcome

to

linuxsimply

How to redirect a file content as input into the “tr” command?

To redirect a file content into the Linux tr command, use the input redirection operator < after the tr command, and mention the file name afterward to manipulate the file in the specified way. For example, to convert the case of any content having uppercase letters to lowercase of file.txt, you can use the command:

tr A-Z a-z < file.txt

file.txt contents:

1 2 3

HELLO

WorlD

Output:

1 2 3

hello

world

What does the “tr” command do in Linux?

The Linux tr command stands for translate. It’s primarily used to translate or delete characters within a stream of data. This command takes input text, processes it according to specified rules, and then outputs the modified text.


Similar Readings

5/5 - (5 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
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

Leave a Comment