1. Convert lowercase to uppercase
$ echo "hello world" | tr 'a-z' 'A-Z'
Convert all lowercase letters to uppercase
Output:
HELLO WORLD
2. Convert uppercase to lowercase
$ echo "HELLO WORLD" | tr 'A-Z' 'a-z'
Convert all uppercase letters to lowercase
Output:
hello world
3. Translate spaces to tabs
$ echo "hello world how are you" | tr ' ' '\t'
Replace all spaces with tab characters
Output:
hello world how are you
4. Translate tabs to spaces
$ printf "hello\tworld\thow\tare\tyou" | tr '\t' ' '
Replace all tab characters with spaces
Output:
hello world how are you
5. Delete specific characters
$ echo "hello, world!" | tr -d ','
Delete all occurrences of comma
Output:
hello world!
6. Delete all digits
$ echo "hello123 world456" | tr -d '0-9'
Delete all digits from the input
Output:
hello world
7. Squeeze repeating characters
$ echo "hellooo wooorld" | tr -s 'o'
Squeeze multiple 'o' characters into one
Output:
hello woorld
8. Squeeze repeating spaces
$ echo "hello world" | tr -s ' '
Squeeze multiple spaces into one
Output:
hello world
9. Delete all non-digit characters
$ echo "hello123 world456" | tr -cd '0-9'
Complement the set and delete all non-digit characters
Output:
123456
10. Translate braces to parentheses
$ echo "{hello world}" | tr '{}' '()'
Replace curly braces with parentheses
Output:
(hello world)
11. Delete all punctuation
$ echo "hello, world! How are you?" | tr -d '[:punct:]'
Delete all punctuation characters
Output:
hello world How are you
12. Translate to rot13 encoding
$ echo "hello world" | tr 'A-Za-z' 'N-ZA-Mn-za-m'
Apply rot13 cipher to the text
Output:
uryyb jbeyq
13. Delete all control characters
$ printf "hello\n\tworld" | tr -d '[:cntrl:]'
Delete all control characters (like newline, tab)
Output:
helloworld
14. Translate newlines to spaces
$ echo -e "hello\nworld\nhow\nare\nyou" | tr '\n' ' '
Replace all newline characters with spaces
Output:
hello world how are you
15. Squeeze repeating characters of any type
$ echo "helloooooo woooorld" | tr -s 'a-z'
Squeeze repeating letters into one
Output:
helo world
16. Delete all whitespace characters
$ echo "hello world" | tr -d '[:space:]'
Delete all whitespace characters
Output:
helloworld
17. Translate using character classes
$ echo "hello123" | tr '[:lower:]' '[:upper:]'
Convert lowercase to uppercase using character classes
Output:
HELLO123
18. Delete all non-printable characters
$ printf "hello\000world" | tr -cd '[:print:]'
Delete all non-printable characters
Output:
helloworld
19. Translate with sequence of characters
$ echo "abc" | tr 'abc' 'XYZ'
Translate a to X, b to Y, c to Z
Output:
XYZ
20. Delete all alphabetic characters
$ echo "hello123 world456" | tr -d '[:alpha:]'
Delete all alphabetic characters
Output:
123 456
21. Translate with character ranges
$ echo "hello" | tr 'a-z' 'A-Z'
Convert lowercase to uppercase using ranges
Output:
HELLO
22. Delete all but alphanumeric characters
$ echo "hello, world! 123" | tr -cd '[:alnum:]'
Delete all non-alphanumeric characters
Output:
helloworld123
23. Translate with octal values
$ echo "hello" | tr 'ell' '\145\154\154'
Translate using octal values of characters
Output:
hello
24. Squeeze and delete specific characters
$ echo "helllllllo world" | tr -s 'l ' 'l'
Squeeze multiple 'l' characters and spaces
Output:
helo world
25. Translate with hexadecimal values
$ echo "abc" | tr 'abc' '\x41\x42\x43'
Translate using hexadecimal values
Output:
ABC
Common tr Options Summary
-d: Delete characters in SET1
-s: Squeeze repeated characters
-c: Complement the set of characters
-t: Truncate SET1 to SET2's length
[:alnum:]: Alphanumeric characters
[:alpha:]: Alphabetic characters
[:digit:]: Numeric characters
[:lower:]: Lowercase letters
[:upper:]: Uppercase letters
[:space:]: Whitespace characters
[:punct:]: Punctuation characters
[:cntrl:]: Control characters
[:print:]: Printable characters