Using the `tr` command (with examples)
The tr
command is a handy utility that allows you to perform character-level translations on a given input. It can be used to replace or delete specific characters, compress consecutive identical characters, and even transform text to upper or lower case. In this article, we will explore several use cases of the tr
command with code examples and explanations.
1: Replace all occurrences of a character in a file
The syntax for this command is tr find_character replace_character < path/to/file
. This command allows you to replace all occurrences of a specific character in a file and print the result.
Motivation: This use case is useful when you need to replace all instances of a particular character with another character within a file.
Example:
Suppose we have a file named example.txt
with the following content:
Hello World!
To replace all occurrences of the letter ’l’ with ‘z’, we can use the following command:
tr 'l' 'z' < example.txt
The output of this command will be:
Hezzo Worzd!
2: Replace all occurrences of a character from another command’s output
To replace all occurrences of a character from another command’s output, we can use the pipe (|
) operator and the echo
command. The syntax is echo text | tr find_character replace_character
.
Motivation: This use case is helpful when you want to perform character replacements on the output of a command without the need for intermediate files.
Example:
Let’s consider a simple example where we want to replace all occurrences of the letter ‘o’ with ‘x’ from the output of the ls
command. We can use the following command:
echo "$(ls)" | tr 'o' 'x'
This command will display the output of ls
with all ‘o’ characters replaced with ‘x’.
3: Map each character of the first set to the corresponding character of the second set
The syntax for this command is tr 'first_set' 'second_set' < path/to/file
. This command allows you to perform a mapping between characters of two sets.
Motivation: This use case is useful when you need to perform character mapping from one set to another. It can be used for various purposes, such as encryption or creating a cipher.
Example:
Consider a scenario where we want to map each vowel in a file to the corresponding letter in the alphabet. Suppose we have a file named vowels.txt
with the following content:
aeiou
To map each vowel to the next letter in the alphabet, we can use the following command:
tr 'aeiou' 'bcdef' < vowels.txt
The output of this command will be:
bcdef
4: Delete all occurrences of the specified set of characters from the input
The syntax for this command is tr -d 'input_characters' < path/to/the_file
. This command allows you to delete all occurrences of a specified set of characters from the input.
Motivation: This use case is handy when you want to remove certain characters from a given input, such as removing punctuation marks or special characters.
Example:
Suppose we have a file named example.txt
with the following content:
Hello! How are you?
To delete all occurrences of punctuation marks from the file, we can use the following command:
tr -d '[:punct:]' < example.txt
The output of this command will be:
Hello How are you
5: Compress a series of identical characters to a single character
The syntax for this command is tr -s 'input_characters' < path/to/file
. This command allows you to compress consecutive identical characters to a single character.
Motivation: This use case is useful when you want to reduce the number of consecutive identical characters in a given input.
Example:
Suppose we have a file named example.txt
with the following content:
Helloooo Worlddd!
To compress consecutive identical characters to a single character, we can use the following command:
tr -s 'o' < example.txt
The output of this command will be:
Helloo Worlddd!
6: Translate the contents of a file to upper-case
The syntax for this command is tr "[:lower:]" "[:upper:]" < path/to/file
. This command allows you to translate the contents of a file to upper-case.
Motivation: This use case is beneficial when you want to convert the text in a file to upper-case letters.
Example:
Suppose we have a file named example.txt
with the following content:
Hello World!
To convert the text to upper-case, we can use the following command:
tr "[:lower:]" "[:upper:]" < example.txt
The output of this command will be:
HELLO WORLD!
7: Strip out non-printable characters from a file
The syntax for this command is tr -cd "[:print:]" < path/to/file
. This command allows you to strip out non-printable characters from a file.
Motivation: This use case is useful when you want to remove non-printable characters from a given input, such as control characters or special symbols.
Example:
Suppose we have a file named example.txt
with the following content:
Hello!🌟 How are you?
To strip out non-printable characters from the file, we can use the following command:
tr -cd "[:print:]" < example.txt
The output of this command will be:
Hello! How are you?
In conclusion, the tr
command is a powerful tool for performing character-level translations. With its various options and arguments, you can easily replace, delete, compress, and transform characters within a given input. Whether you need to manipulate text files or process command output, the tr
command provides a convenient way to achieve your desired results.