The Linux command tr
is a tool that makes it easy and quick to perform character substitution and deletion tasks, often used to modify file contents or filter text. In this post, we’ll cover the basic usage of the tr
command, its options, and practical ways to use it in real-life scenarios.
Table of Contents
What is the Linux Command tr?
The tr
command stands for translate, and it is used to transform or delete characters from the input text. It is mainly used to substitute characters in a string, replace spaces with other characters, convert lowercase letters to uppercase, and more.
This command is a standard utility available in Linux/Unix environments and is often used in combination with a pipeline (|
) to perform complex text processing tasks in a simple way.
Basic Usage
The tr
command is primarily used for two main purposes:
- Character transformation
- Character deletion
The basic syntax of the command is as follows:
tr [options] 'source character set' 'replacement character set'
ShellScriptExample 1: Converting Lowercase to Uppercase
To convert lowercase letters to uppercase, you can use the following command:
echo "hello world" | tr 'a-z' 'A-Z'
ShellScriptThis will output the string “hello world” with all lowercase letters transformed into uppercase:
Example 2: Replacing Spaces with Underscores
To replace spaces with underscores, you can use the following command:
echo "hello world" | tr ' ' '_'
ShellScriptAs shown below, the spaces in the string are replaced with underscores:
Example 3: Deleting Specific Characters
To delete characters, use the -d
option. For instance, if you want to remove all digits, you can run:
echo "abc123def456" | tr -d '0-9'
ShellScriptThe result will show the string with all numbers removed:
Key Options Explanation
The tr
command offers various options, but understanding a few key ones is sufficient for most use cases.
-d Option
The -d
option stands for delete and is used to delete specific characters or character sets. As seen in the example above, you can easily remove unnecessary characters like digits.
tr -d '0-9' < file.txt
ShellScriptThe command above will delete all numbers from the file.txt
file and display the result.
-s Option
The -s
option stands for squeeze, and it compresses repeated occurrences of characters into a single instance. This is especially useful when you want to reduce multiple spaces to just one.
echo "hello world" | tr -s ' '
ShellScriptThe result shows that consecutive spaces have been reduced to a single space:
-c Option
The -c
option stands for complement, which allows you to specify the reverse of the character set. For instance, if you want to delete everything except alphabetic characters, you can use -cd 'a-zA-Z'
.
echo "hello123" | tr -cd 'a-zA-Z'
ShellScriptThis command will delete everything that isn’t a letter, leaving only the alphabetic characters:
Precautions
- Character Set Size Mismatch: When using the
tr
command, if the size of the source character set and the replacement character set don’t match, errors may occur. For example, trying to replace ‘abc’ with ‘AB’ will not work as expected. Ensure both character sets are the same size. - Pipeline Use: The
tr
command is often used in a pipeline with other commands rather than by itself. To avoid modifying files directly, use the command to verify the results first and then use the>
operator to save the output if necessary. - Multibyte Characters: The
tr
command is designed to handle ASCII characters, so it may not be suitable for multibyte characters like Korean. When working with multibyte characters, tools likesed
orawk
might be better options.
Summary
The Linux command tr
is a highly useful tool for performing simple character transformations in Linux. It can be used for tasks such as converting lowercase letters to uppercase, deleting specific characters, or compressing repeated spaces. By understanding a few basic commands and options, you can greatly enhance your ability to manage text data. However, be cautious when dealing with multibyte characters, and always verify results through a pipeline before applying them to files.
Now, try using the tr
command to manage your text data more efficiently!