Linux Command tr and 3 Options

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.

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:

  1. Character transformation
  2. Character deletion

The basic syntax of the command is as follows:

tr [options] 'source character set' 'replacement character set'
ShellScript

Example 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'
ShellScript

This will output the string “hello world” with all lowercase letters transformed into uppercase:

Figure 1. Linux command tr: converting lowercase to uppercase
Figure 1. Linux command tr: converting lowercase to uppercase

Example 2: Replacing Spaces with Underscores

To replace spaces with underscores, you can use the following command:

echo "hello world" | tr ' ' '_'
ShellScript

As shown below, the spaces in the string are replaced with underscores:

Figure 2. Linux command tr: replacing spaces with underscores
Figure 2. Linux command tr: replacing spaces 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'
ShellScript

The result will show the string with all numbers removed:

Figure 3. Linux command tr: deleting numbers from a string
Figure 3. Linux command tr: deleting numbers from a string

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
ShellScript

The command above will delete all numbers from the file.txt file and display the result.

Figure 4. Linux command tr: using the -d option to delete specific characters or character sets
Figure 4. Linux command tr: using the -d option to delete specific characters or character sets

-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 ' '
ShellScript

The result shows that consecutive spaces have been reduced to a single space:

Figure 5. Linux command tr: using the -s option to compress repeated characters
Figure 5. Linux command tr: using the -s option to squeeze repeated characters

-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'
ShellScript

This command will delete everything that isn’t a letter, leaving only the alphabetic characters:

Figure 6. Linux command tr: using the -c option to specify non-character sets
Figure 6. Linux command tr: using the -c option to specify non-character sets

Precautions

  1. 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.
  2. 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.
  3. 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 like sed or awk 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!

References

Leave a Comment