Or copy link
Copy link
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.
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.
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:
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.
Explore the tr Command With Ultahost Linux VPS!
Ultahost offers Linux hosting with NVMe SSD storage, making it easy to experiment with Linux commands. Use our cheap Linux VPS to practice and improve your command-line skills efficiently.
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
Let’s explore practical examples of the tr Linux command to get a better understanding.
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:
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:
Read also Exploring chsh Command in Linux with Examples
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:
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:
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:
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'
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:
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:]
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:]
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:
logs.txt
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.
newLogFile.txt
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.
The tr command in Linux is used to translate or delete characters in text. It can convert text cases, squeeze repeated characters, delete specific characters, and perform basic find-and-replace operations.
The tr command cannot read files directly. To use it with files, we need to pipe the file content into tr or redirect the file to stdin.
The -c option complements the given set of characters, applying the operation to characters not in the specified set.
We can use the -s option with tr to squeeze consecutive repeated characters into a single occurrence.
tr itself does not directly redirect output to a file. However, you can use shell redirection to save the output to a file.
The -d option deletes all occurrences of the specified characters from the output.
You can convert text cases by specifying the character ranges to translate, such as converting lowercase to uppercase or vice versa.
Port forwarding is a crucial technique for network admi...
Linux operating systems use the "systemctl" command to ...
The Linux IP command is a powerful tool used for managi...
In Linux, you can efficiently manage your system by che...
GCC (GNU Compiler Collection) is a widely used compiler...
Kali Linux is a powerful and versatile operating system...
Save my name, email, and website in this browser for the next time I comment.
Δ