tr
tr
is a command-line utility used to translate or delete characters from standard input and write the result to standard output. It is commonly used for transforming text, such as changing case, deleting characters, or replacing characters.
Basic Syntax
Commonly Used Options
-c
: Complement the set of characters inset1
.-d
: Delete characters inset1
.-s
: Squeeze repeated characters inset1
.-t
: Truncateset1
to the length ofset2
.
Examples
Basic Translation
Translate lowercase letters to uppercase:
Delete Characters
Delete all digits from the input:
Squeeze Repeated Characters
Squeeze multiple spaces into a single space:
Complement Set
Replace all characters except digits with asterisks:
Translate and Delete
Translate lowercase to uppercase and delete digits:
Using Character Classes
Translate all lowercase letters to uppercase using character classes:
Using Ranges
Translate digits to letters:
Truncate Set1
Translate the first three letters to uppercase, ignoring the rest:
Conclusion
tr
is a versatile tool for translating, deleting, and squeezing characters in text. Understanding its options allows for efficient and powerful text manipulation directly from the command line.