Click on each book below to review & buy on Amazon.
As an Amazon Associate, I earn from qualifying purchases.
CompTIA Linux+ XK0-005 - 3.1 - Common Script Utilities: tr
The tr
command in Linux is a versatile utility used for translating or deleting characters. It is primarily used to transform or manipulate text by replacing specified characters with other characters or deleting them altogether. The tr
command operates on a per-character basis and can be applied to both files and standard input. In this guide, we will explore the various aspects of the tr
command, including its purpose, common options, real-world examples, and a conclusion summarizing its utility.
Explanation
The tr
command is designed to perform character translation or deletion in text streams. It reads input from a file or standard input, applies specified transformations, and outputs the modified text. The transformations can involve replacing specific characters with other characters, deleting certain characters, or squeezing consecutive occurrences of characters into a single instance.
Common Command Options
- -c, -C, --complement: Inverts the set of characters specified for translation.
- -d, --delete: Deletes characters specified in the set.
- -s, --squeeze-repeats: Squeezes consecutive occurrences of the same character into a single instance.
- -t, --truncate-set1: Truncates set1 to the length of set2.
Real World Examples
Replacing characters
echo "Hello, World!" | tr 'o' 'a'
This command replaces all occurrences of the letter 'o' with the letter 'a' in the given text. The output will be: "Hella, Warld!"
Deleting characters
echo "DTV Linux is amazing!" | tr -d 'aeiou'
This command deletes all occurrences of the vowels 'a,' 'e,' 'i,' 'o,' and 'u' in the given text. The output will be: "DTV Lnx s mzng!"
Squeezing repeated characters
echo "Hello, World!" | tr -s ' '
This command squeezes consecutive spaces into a single space in the given text. The output will be: "Hello, World!"
Replacing character ranges
echo "123456789" | tr '0-9' 'a-j'
This command replaces all digits with corresponding lowercase letters in the given text. The output will be: "abcdefghi"
Conclusion
The tr
command is a powerful tool for character translation and deletion in Linux. It provides a flexible way to manipulate text by replacing specific characters, deleting unwanted characters, or squeezing repeated characters. With its wide range of options and transformations, the tr
command proves to be a valuable utility for various text processing tasks, such as data cleaning, text manipulation, and data transformation. By mastering the tr
command, you can efficiently handle character-based operations and streamline your text processing workflows in the Linux environment.
Support DTV Linux
Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.
NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.