Linux tr Guide - ryzendew/Linux-Tips-and-Tricks GitHub Wiki
Linux tr Guide
Complete beginner-friendly guide to tr on Linux, covering Arch Linux, CachyOS, and other distributions including character translation, text transformation, and character manipulation.
Table of Contents
Understanding tr
What is tr?
tr (translate) translates or deletes characters.
Uses:
- Character translation: Replace characters
- Case conversion: Change case
- Character deletion: Remove characters
- Text transformation: Transform text
Why it matters:
- Text processing: Transform text
- Data cleaning: Clean up data
- Scripting: Useful in scripts
tr Basics
Translate Characters
Basic usage:
# Translate characters
echo "hello" | tr 'e' 'E'
# Output: hEllo
Case Conversion
Change case:
# Lowercase to uppercase
echo "hello" | tr 'a-z' 'A-Z'
# Output: HELLO
Character Translation
Character Sets
Use sets:
# Translate set
echo "hello123" | tr 'a-z' 'A-Z'
# Output: HELLO123
Delete Characters
Remove characters:
# Delete characters
echo "hello123" | tr -d '0-9'
# -d = delete
# Output: hello
Text Transformation
Squeeze Characters
Remove duplicates:
# Squeeze repeated
echo "hello world" | tr -s ' '
# -s = squeeze
# Output: hello world
Complement
Invert selection:
# Delete non-digits
echo "hello123" | tr -cd '0-9'
# -c = complement, -d = delete
# Output: 123
Troubleshooting
tr Not Found
Check installation:
# Check tr
which tr
# Usually in coreutils
# Install if missing
sudo pacman -S coreutils
Summary
This guide covered tr usage, character translation, and text transformation for Arch Linux, CachyOS, and other distributions.
Next Steps
- sed and awk Guide - Text processing
- cut Guide - Field extraction
- tr Documentation:
man tr
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.