Exploring the Linux tr Command

tr is a text manipulation command in Linux/Unix that is used to translate or delete characters. It performs various transformations, such as converting uppercase to lowercase (or vice versa), squeezing repeated characters, deleting specific characters, and performing basic find-and-replace operations. You can use the tr command with Unix pipes to perform more complex operations or transformations.

This command is a part of the GNU Coreutils package and is available in all Linux distributions by default. In this tutorial, we will explore the Linux tr command with practical examples.

What is tr Command in Linux?

The name tr is an abbreviation of “translate”. The tr command reads a byte stream from stdin, performs transformations like translating characters or deleting specific ones, and writes the result to stdout. The tr command cannot read files directly. To use tr on a file, you should either pipe the file’s content into tr or redirect the file to stdin. These methods enable tr to process the content of the file.

How to Use the tr Command in Linux?

To use this command in Linux, you must follow the below-provided syntax:

tr [OPTION] SET1 [SET2]

The OPTION can be one of the following:

  • c: It complements the given set (applies the operation to characters not in set1).
  • d: It deletes the characters from the output that appears in set1.
  • s: It replaces consecutive repeated characters in set1 with a single occurrence.
  • t: It truncates set1 to match the length of set2.

If you run tr without any options, it will replace each character in SET1 with the corresponding character in SET2 based on their position. The SETs in the above syntax refer to strings of characters used for matching and transformation.

To learn more about the tr command, its options, and interpreted sequences for character matching, you can refer to its official manual page, which can be accessed using the following command:

man tr
tr command manual page

Let’s explore practical examples of the tr Linux command to get a better understanding.

Example 1: Case Conversion Using Linux tr Command

In the following example, we use the tr command to convert all lowercase letters in the given string to uppercase:

echo "Hi, I am Anees Asghar, a Technical Content Writer at Ultahost.com" | tr 'a-z' 'A-Z'

We use the echo command to output a string, which is then piped (|) into the tr command, which translates uppercase letters to their lowercase equivalents:

lowercase to uppercase using tr

Similarly, we can use the tr command for uppercase to lowercase conversion, as demonstrated in the following example:

echo "HI, WELCOME TO ULTAHOST.COM" | tr 'A-Z' 'a-z'

The output shows that the given string has been successfully converted into lowercase:

upper to lowercase using tr

Example 2: Redirecting the Output of the tr Command to a File

The tr command itself cannot directly redirect its output to a file, however, we can use shell redirection to achieve this. For instance, in the following example we use the tr command to translate the white spaces to tabs and redirect the output to the sampleFile.txt:

echo "HI, WELCOME TO ULTAHOST.COM" | tr ' ' '\t' > sampleFile.txt

We can verify the output by running the cat command, as shown in the following screenshot:

redirecting the output to a file

Example 3: Replacing a Specific Character in a String Using the tr Command

We can replace a specific character with another character using the tr command. In the following example, we replace a whitespace “ ” with “u” using the tr command:

echo " ltahost.com" | tr ' ' u

The output below shows that the whitespace has been successfully replaced with u at the corresponding position:

replacing a specific character using tr

Example 4: Removing Repeating Characters From a String Using tr

We can use the tr command with the -s option to remove consecutive repeated characters from a string. This option squeezes repeated characters into a single occurrence, as demonstrated in the following example:

echo "wwelcome to uultahost.ccom" | tr -s 'a-z'
removing repetitive characters using tr

Example 5: Removing a Specific Character From a String Using tr

We can use the tr command with the “-d” option to remove all the occurrences of a specific character from the given string:

echo "Hi, Welcome to ultahost.com" | tr -d o

In this example, we delete all occurrences of the character “o” from the given string:

removing a specific character

Example 6: Removing All Digits From a String Using tr

Run the tr command with the -d option to delete all digits from an alphanumeric string:

echo "My email id is [email protected]" | tr -d [:digit:]
removing all digits using tr

Example 7: Complementing the Characters in the SET1 Using tr

We can use the -c option with the tr command to complement the characters in the SET1. For instance, we will use the c option in the above example, and as a result, it will remove all characters instead of digits:

echo "My email id is [email protected]" | tr -cd [:digit:]
complementing a set using tr

Example 8: Using tr Command in a Bash Script

We can also use the tr command in the bash script to enhance text manipulation capabilities. For example, we have a log file named logs.txt, which contains mixed-case and messy text:

ERROR:   File NOT FOUND
WARNING:    Disk space LOW
Info:  System UPDATE SUCCESSFUL

Suppose, we want to clean it up by converting all text to lowercase and removing extra whitespaces. For this purpose, we can use the following bash tr command:

#!/bin/bash
cat logs.txt | tr 'A-Z' 'a-z' | tr -s ' '
cat logs.txt | tr 'A-Z' 'a-z' | tr -s ' ' > newLogFile.txt

This script will convert text to lowercase, remove extra spaces from the log file, and save the cleaned output to a new file named newLogFile.txt.

Conclusion

The tr command is a text manipulation command that helps us translate and remove characters from the given text. Moreover, we can use it to convert text between uppercase and lowercase, remove specific characters, and handle repeated characters. Although tr can’t read files directly, we can use it with pipes and redirection to process text from files. This guide explored various examples to show how the tr command works in Linux.

The tr command is a useful Linux tool for translating, deleting, or squeezing characters in text streams. For a more flexible environment, consider Ultahost’s VDS hosting plans which provide dedicated resources and optimal performance for advanced tasks.

FAQ

What is the tr command in Linux?
Can we use the tr command to process the file’s content?
What does the -c option do in the tr command?
How do you remove repeated characters using tr?
Can tr redirect its output to a file?
What is the purpose of the -d option in tr?
How do you convert text cases using the tr command?

Related Post

How to Forward Ports With Iptables in Linux

Port forwarding is a crucial technique for network admi...

How to Fix Systemctl Command Not Found Error

Linux operating systems use the "systemctl" command to ...

Exploring Linux ip Command with Examples

The Linux IP command is a powerful tool used for managi...

How to Check Running Processes in Linux?

In Linux, you can efficiently manage your system by che...

How to Install GCC Compiler on Linux

GCC (GNU Compiler Collection) is a widely used compiler...

How to Fix Update and Upgrade Issues in Kali

Kali Linux is a powerful and versatile operating system...

Leave a Comment