LFCS Part 38: Text Transformation with tr

Master the tr command for character-by-character text transformation. Learn case conversion, character deletion, squeezing repeats, and complement sets for efficient text processing.

21 min read

After mastering regular expressions in the previous posts, you might wonder: "Is there a simpler tool for basic text transformations?" The answer is yes - meet the tr command!

While regex excels at pattern matching and sed/awk handle complex text processing, tr is perfect for simple, character-by-character transformations. It's fast, efficient, and easy to use for tasks like case conversion, character deletion, and squeezing repeated characters.

What You'll Learn

In this comprehensive guide, you'll master:

  • What tr is and when to use it
  • Character translation (replacing characters)
  • Case conversion (uppercase/lowercase)
  • Deleting characters with -d
  • Squeezing repeats with -s
  • Complement sets with -c
  • Character ranges and special sets
  • Combining tr with pipes
  • Real-world text cleanup tasks
  • 20 hands-on practice labs

Part 1: Understanding tr

What is tr?

tr stands for translate or transliterate. It's a simple utility that translates, deletes, or squeezes characters from standard input and writes to standard output.

Key characteristics:

  • Works character-by-character (not pattern-based)
  • Reads from stdin, writes to stdout
  • Simple and fast
  • Perfect for basic transformations

Basic syntax:

tr [OPTIONS] SET1 [SET2]

tr vs sed vs awk

| Tool | Best For | Example | |------|----------|---------| | tr | Simple character transformations | Convert case, delete spaces | | sed | Line-based pattern replacement | Complex substitutions | | awk | Field-based processing | Column extraction, calculations |

When to use tr:

  • Converting case
  • Removing specific characters
  • Squeezing repeated characters
  • Simple character substitutions

When NOT to use tr:

  • Pattern matching (use grep/sed)
  • Field-based operations (use awk)
  • Complex transformations (use sed/awk)

Part 2: Basic Character Translation

Simple Character Replacement

Example 1: Replace one character with another

# Replace 'a' with 'x'
echo "apple banana" | tr 'a' 'x'

Output:

xpple bxnxnx

Example 2: Multiple character replacements

# Replace 'a' with 'x' AND 'e' with 'y'
echo "apple banana" | tr 'ae' 'xy'

Output:

xpply bxnxnx

How it works:

  • First character in SET1 (a) → First character in SET2 (x)
  • Second character in SET1 (e) → Second character in SET2 (y)

Example 3: Translating multiple characters

# Create test file
echo "hello world" > test.txt

# Replace vowels with numbers
cat test.txt | tr 'aeiou' '12345'

Output:

h2ll4 w4rld

Mapping:

  • a1
  • e2
  • i3
  • o4
  • u5

Part 3: Case Conversion

One of the most common uses of tr is converting case.

Lowercase to Uppercase

Example 4: Convert to uppercase

# Method 1: Using character ranges
echo "hello world" | tr 'a-z' 'A-Z'

# Method 2: Using predefined sets
echo "hello world" | tr '[:lower:]' '[:upper:]'

Output:

HELLO WORLD
HELLO WORLD

Uppercase to Lowercase

Example 5: Convert to lowercase

# Method 1: Using character ranges
echo "HELLO WORLD" | tr 'A-Z' 'a-z'

# Method 2: Using predefined sets
echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'

Output:

hello world
hello world

Real-World Case Conversion

Example 6: Normalize filenames

# Create files with mixed case
touch MyFile.TXT DOCUMENT.PDF test.jpg

# Convert all to lowercase for consistency
for file in *; do
    newname=$(echo "$file" | tr 'A-Z' 'a-z')
    mv "$file" "$newname" 2>/dev/null
done

# Result: myfile.txt document.pdf test.jpg

Example 7: Normalize user input

# Get user input and convert to lowercase
read -p "Enter username: " username
username=$(echo "$username" | tr 'A-Z' 'a-z')
echo "Normalized: $username"

Part 4: Character Ranges and Sets

Using Character Ranges

Syntax: [start-end]

Example 8: Common ranges

# Lowercase letters
echo "abc123XYZ" | tr 'a-z' 'A-Z'
# Output: ABC123XYZ

# Digits
echo "abc123def456" | tr '0-9' 'X'
# Output: abcXXXdefXXX

# Uppercase letters
echo "Hello World 123" | tr 'A-Z' '*'
# Output: *ello *orld 123

Predefined Character Classes

POSIX character classes for portable scripts:

| Class | Description | Equivalent | |-------|-------------|------------| | [:alnum:] | Alphanumeric | [a-zA-Z0-9] | | [:alpha:] | Alphabetic | [a-zA-Z] | | [:digit:] | Digits | [0-9] | | [:lower:] | Lowercase | [a-z] | | [:upper:] | Uppercase | [A-Z] | | [:space:] | Whitespace | Space, tab, newline | | [:punct:] | Punctuation | . , ; : ! ? etc. | | [:blank:] | Space and tab | \t |

Example 9: Using character classes

# Remove all digits
echo "User123 has 456 points" | tr -d '[:digit:]'
# Output: User has  points

# Replace all punctuation with space
echo "Hello, world! How are you?" | tr '[:punct:]' ' '
# Output: Hello  world  How are you

# Convert all alphanumeric to asterisks
echo "Secret123!" | tr '[:alnum:]' '*'
# Output: ********!

Part 5: Deleting Characters

The -d option deletes specified characters.

Syntax: tr -d SET1

Basic Deletion

Example 10: Remove specific characters

# Remove all spaces
echo "hello world test" | tr -d ' '
# Output: helloworldtest

# Remove all vowels
echo "hello world" | tr -d 'aeiou'
# Output: hll wrld

# Remove digits
echo "User123 logged in" | tr -d '0-9'
# Output: User logged in

Example 11: Remove multiple character types

# Remove spaces and punctuation
echo "Hello, world! How are you?" | tr -d ' ,.!?'
# Output: HelloworldHowareyou

# Remove all non-alphanumeric characters
echo "Phone: (555) 123-4567" | tr -d -c '[:alnum:]'
# Output: 5551234567

Practical Deletion Examples

Example 12: Clean up CSV data

# Create CSV with extra quotes
echo '"John","Doe","555-1234"' > data.csv

# Remove quotes
cat data.csv | tr -d '"'
# Output: John,Doe,555-1234

Example 13: Remove line breaks

# Create multi-line text
cat > multiline.txt << 'EOF'
This is
a multi-line
text file
EOF

# Remove newlines (convert to single line)
cat multiline.txt | tr -d '\n'
# Output: This isa multi-linetext file

Example 14: Clean phone numbers

# Remove formatting from phone numbers
echo "(555) 123-4567" | tr -d ' ()-'
# Output: 5551234567

Part 6: Squeezing Repeated Characters

The -s option squeezes (collapses) repeated characters into a single occurrence.

Syntax: tr -s SET1

Basic Squeezing

Example 15: Remove duplicate spaces

# Multiple spaces become single space
echo "hello    world     test" | tr -s ' '
# Output: hello world test

Example 16: Squeeze specific characters

# Squeeze repeated 'l's
echo "hello" | tr -s 'l'
# Output: helo

# Squeeze repeated digits
echo "Error 1112223334" | tr -s '0-9'
# Output: Error 1234

Practical Squeezing Examples

Example 17: Clean up whitespace

# Create file with messy spacing
cat > messy.txt << 'EOF'
This  has   too    many     spaces
And  tabs		here
EOF

# Squeeze all whitespace to single spaces
cat messy.txt | tr -s '[:space:]' ' '
# Output: This has too many spaces
#         And tabs here

Example 18: Clean log files

# Create log with duplicate blank lines
cat > app.log << 'EOF'
Line 1


Line 2



Line 3
EOF

# Squeeze blank lines
cat app.log | tr -s '\n'
# Output: Line 1
#         Line 2
#         Line 3

Part 7: Complement Sets

The -c option uses the complement of SET1 (everything NOT in SET1).

Syntax: tr -c SET1 SET2

Understanding Complement

Example 19: Keep only letters

# Replace everything that's NOT a letter with space
echo "Hello123 World456!" | tr -c '[:alpha:]' ' '
# Output: Hello    World

Example 20: Keep only digits

# Replace everything that's NOT a digit with nothing (delete)
echo "Price: $123.45" | tr -cd '[:digit:]'
# Output: 12345

Example 21: Extract only alphanumeric

# Keep only letters and numbers
echo "User@123_test!" | tr -cd '[:alnum:]'
# Output: User123test

Combining Flags

You can combine -c, -d, and -s:

Example 22: Complex transformations

# Keep only letters and squeeze spaces
echo "Hello,,,, World!!!" | tr -cs '[:alpha:]' ' '
# Output: Hello World

# Remove non-digits and squeeze
echo "Phone: (555)  123-4567" | tr -csd '[:digit:]' ''
# Output: 5551234567

Part 8: Advanced Techniques

Translating Special Characters

Example 23: Convert line endings

# Convert Windows line endings (CRLF) to Unix (LF)
cat windows_file.txt | tr -d '\r' > unix_file.txt

# Convert spaces to newlines
echo "one two three four" | tr ' ' '\n'
# Output: one
#         two
#         three
#         four

Example 24: Escape sequences

# Tab to space
echo -e "col1\tcol2\tcol3" | tr '\t' ' '
# Output: col1 col2 col3

# Newline to comma
cat file.txt | tr '\n' ',' | sed 's/,$/\n/'

Using tr in Pipelines

Example 25: Complex pipeline

# Process log file:
# 1. Convert to lowercase
# 2. Remove punctuation
# 3. Squeeze spaces
cat logfile.txt | tr 'A-Z' 'a-z' | tr -d '[:punct:]' | tr -s ' '

Example 26: Word frequency analysis

# Count word frequency
cat document.txt | \
    tr -cs '[:alpha:]' '\n' | \
    tr 'A-Z' 'a-z' | \
    sort | \
    uniq -c | \
    sort -rn | \
    head -10

Explanation:

  1. tr -cs '[:alpha:]' '\n' - Keep only letters, convert non-letters to newlines
  2. tr 'A-Z' 'a-z' - Convert to lowercase
  3. sort - Sort words
  4. uniq -c - Count occurrences
  5. sort -rn - Sort by count (descending)
  6. head -10 - Top 10 words

Part 9: Real-World Scenarios

Scenario 1: Password Cleanup

Task: Remove invalid characters from passwords

# Only allow alphanumeric and specific symbols
read -sp "Enter password: " password
clean_password=$(echo "$password" | tr -cd '[:alnum:]@#$%^&*')
echo "Cleaned password length: ${#clean_password}"

Scenario 2: Data Sanitization

Task: Clean CSV data before import

# Create messy CSV
cat > messy.csv << 'EOF'
"John  Doe","  email@test.com  ","(555) 123-4567"
"Jane  Smith","admin@site.org","555-987-6543  "
EOF

# Clean: remove quotes, squeeze spaces, trim
cat messy.csv | tr -d '"' | tr -s ' ' | sed 's/^ //;s/ $//'

Scenario 3: Log Analysis

Task: Extract error codes from logs

# Create log
cat > error.log << 'EOF'
[ERROR-404] Page not found
[WARNING-301] Redirect
[ERROR-500] Internal server error
EOF

# Extract only error codes
cat error.log | tr -cd '[:digit:]\n'
# Output: 404
#         301
#         500

Scenario 4: URL Encoding Preparation

Task: Prepare strings for URL encoding

# Replace spaces with underscores for URL-friendly names
echo "My Document File.pdf" | tr ' ' '_' | tr 'A-Z' 'a-z'
# Output: my_document_file.pdf

Practice Labs

Time to practice! Complete these 20 hands-on labs.

Warm-up Labs (1-5): Basic Transformations

Lab 1: Character Translation

Task: Use tr to:

  • Replace 'a' with 'x'
  • Replace all vowels with asterisks
  • Replace digits with '#'
Solution
# Replace 'a' with 'x'
echo "banana apple" | tr 'a' 'x'
# Output: bxnxnx xpple

# Replace vowels with asterisks
echo "hello world" | tr 'aeiou' '*'
# Output: h*ll* w*rld

# Replace digits with '#'
echo "User123 has 456 points" | tr '0-9' '#'
# Output: User### has ### points

Lab 2: Case Conversion

Task: Convert text to:

  • All uppercase
  • All lowercase
  • Using both character ranges and POSIX classes
Solution
# To uppercase (character range)
echo "hello world" | tr 'a-z' 'A-Z'
# Output: HELLO WORLD

# To uppercase (POSIX class)
echo "hello world" | tr '[:lower:]' '[:upper:]'
# Output: HELLO WORLD

# To lowercase (character range)
echo "HELLO WORLD" | tr 'A-Z' 'a-z'
# Output: hello world

# To lowercase (POSIX class)
echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'
# Output: hello world

Lab 3: Character Deletion

Task: Delete:

  • All spaces
  • All digits
  • All punctuation
Solution
# Delete spaces
echo "hello world test" | tr -d ' '
# Output: helloworldtest

# Delete digits
echo "User123 logged at 10:30" | tr -d '0-9'
# Output: User logged at :

# Delete punctuation
echo "Hello, world! How are you?" | tr -d '[:punct:]'
# Output: Hello world How are you

Lab 4: Squeezing Characters

Task: Squeeze:

  • Multiple spaces to single space
  • Repeated characters
  • Multiple blank lines
Solution
# Squeeze multiple spaces
echo "too    many     spaces" | tr -s ' '
# Output: too many spaces

# Squeeze repeated 'o'
echo "goooood fooood" | tr -s 'o'
# Output: god fod

# Squeeze blank lines
printf "line1\n\n\nline2\n\n\n\nline3\n" | tr -s '\n'
# Output: line1
#         line2
#         line3

Lab 5: Using Character Ranges

Task: Use ranges to:

  • Convert lowercase to uppercase
  • Replace all letters with 'X'
  • Replace digits 0-5 with 'A' and 6-9 with 'B'
Solution
# Lowercase to uppercase
echo "hello123" | tr 'a-z' 'A-Z'
# Output: HELLO123

# All letters to 'X'
echo "Hello World 123" | tr 'a-zA-Z' 'X'
# Output: XXXXX XXXXX 123

# Digits 0-5 → 'A', 6-9 → 'B'
echo "012345 6789" | tr '0-5' 'A' | tr '6-9' 'B'
# Output: AAAAAA BBBB

Core Labs (6-13): Practical Applications

Lab 6: Clean Phone Numbers

Task: Remove formatting from phone numbers:

  • Remove spaces, dashes, parentheses
  • Keep only digits
Solution
# Create phone numbers
cat > phones.txt << 'EOF'
(555) 123-4567
555-987-6543
+1 (800) 555-0199
EOF

# Clean phone numbers
cat phones.txt | tr -cd '[:digit:]\n'
# Output: 5551234567
#         5559876543
#         18005550199

Lab 7: ROT13 Cipher

Task: Implement ROT13 encoding (rotate letters by 13 positions)

Solution
# Encode with ROT13
echo "Hello World" | tr 'A-Za-z' 'N-ZA-Mn-za-m'
# Output: Uryyb Jbeyq

# Decode (ROT13 is symmetric - apply twice to get original)
echo "Uryyb Jbeyq" | tr 'A-Za-z' 'N-ZA-Mn-za-m'
# Output: Hello World

Explanation:

  • A-MN-Z (shift by 13)
  • N-ZA-M (wrap around)
  • Same for lowercase

Lab 8: CSV Quote Removal

Task: Clean CSV data by removing quotes and extra spaces

Solution
# Create CSV with quotes
cat > data.csv << 'EOF'
"John","Doe","555-1234"
"Jane  ","Smith  ","555-5678"
"Bob","Jones  ","555-9012"
EOF

# Remove quotes and squeeze spaces
cat data.csv | tr -d '"' | tr -s ' '
# Output: John,Doe,555-1234
#         Jane ,Smith ,555-5678
#         Bob,Jones ,555-9012

Lab 9: Convert Tabs to Spaces

Task: Convert all tabs to spaces (4 spaces per tab)

Solution
# Create file with tabs
printf "col1\tcol2\tcol3\n" > tabbed.txt
printf "data1\tdata2\tdata3\n" >> tabbed.txt

# Convert tabs to 4 spaces
cat tabbed.txt | tr '\t' ' ' # Single space
# Or use expand for proper tab stops
expand -t 4 tabbed.txt

# Simple tr (1:1 mapping only)
cat tabbed.txt | tr '\t' ' '
# Output: col1 col2 col3
#         data1 data2 data3

Lab 10: Extract Alphanumeric Only

Task: Keep only letters and numbers, remove everything else

Solution
# Create messy text
echo "User@123#Name$456%Test!" > messy.txt

# Keep only alphanumeric
cat messy.txt | tr -cd '[:alnum:]\n'
# Output: User123Name456Test

Lab 11: Normalize Whitespace

Task: Replace all whitespace (spaces, tabs, newlines) with single spaces

Solution
# Create file with mixed whitespace
cat > whitespace.txt << 'EOF'
word1  	word2
word3		word4
EOF

# Normalize all whitespace
cat whitespace.txt | tr -s '[:space:]' ' '
# Output: word1 word2 word3 word4

Lab 12: Create Word List

Task: Convert text to one word per line

Solution
# Create sentence
echo "The quick brown fox jumps over the lazy dog" > sentence.txt

# Convert to word list
cat sentence.txt | tr -cs '[:alpha:]' '\n' | grep -v '^$'
# Output: The
#         quick
#         brown
#         fox
#         ...

Explanation:

  • -c - complement (everything NOT alpha)
  • -s - squeeze
  • Convert non-letters to newlines

Lab 13: Path Formatting

Task: Convert Windows paths to Unix paths

Solution
# Windows path
echo 'C:\Users\John\Documents\file.txt' | tr '\\' '/'
# Output: C:/Users/John/Documents/file.txt

# Convert drive letter to lowercase
echo 'C:\Users\John\Documents' | tr '\\' '/' | tr 'A-Z:' 'a-z '
# Output: c /users/john/documents

Advanced Labs (14-20): Complex Scenarios

Lab 14: Password Strength Checker

Task: Check if password contains required character types

Solution
# Test password
password="MyP@ss123"

# Check for uppercase
echo "$password" | tr -cd '[:upper:]' | grep -q . && echo "Has uppercase" || echo "No uppercase"

# Check for lowercase
echo "$password" | tr -cd '[:lower:]' | grep -q . && echo "Has lowercase" || echo "No lowercase"

# Check for digits
echo "$password" | tr -cd '[:digit:]' | grep -q . && echo "Has digits" || echo "No digits"

# Check for special chars
echo "$password" | tr -cd '[:punct:]' | grep -q . && echo "Has special" || echo "No special"

Output:

Has uppercase
Has lowercase
Has digits
Has special

Lab 15: Log File Cleanup

Task: Clean log files by:

  • Removing control characters
  • Squeezing blank lines
  • Normalizing spaces
Solution
# Create messy log
cat > messy.log << 'EOF'
[INFO]  Application   started


[ERROR]  Connection    failed


[INFO]  Retry   attempt
EOF

# Clean log
cat messy.log | tr -d '\r' | tr -s ' ' | tr -s '\n'
# Output: [INFO] Application started
#         [ERROR] Connection failed
#         [INFO] Retry attempt

Lab 16: Email Address Normalization

Task: Normalize email addresses (lowercase, trim spaces)

Solution
# Create email list
cat > emails.txt << 'EOF'
  User@EXAMPLE.COM
Admin@Site.ORG
test@DOMAIN.net
EOF

# Normalize
cat emails.txt | tr 'A-Z' 'a-z' | tr -d ' '
# Output: user@example.com
#         admin@site.org
#         test@domain.net

Lab 17: Data Masking

Task: Mask sensitive data (replace digits and letters with asterisks)

Solution
# Credit card number
ccnum="1234-5678-9012-3456"

# Mask all but last 4 digits
masked=$(echo "$ccnum" | sed 's/^.*\(.{4}\)$/\1/' | \
         echo "$ccnum" | tr '0-9' '*' | sed "s/....$/$(echo "$ccnum" | tail -c 5)/")

# Simple version: mask all digits
echo "$ccnum" | tr '0-9' '*'
# Output: ****-****-****-****

# Mask letters in name
echo "John Doe" | tr 'a-zA-Z' '*'
# Output: **** ***

Lab 18: Configuration File Processing

Task: Process config files:

  • Remove comments (lines starting with #)
  • Remove inline comments
  • Squeeze spaces
Solution
# Create config
cat > app.conf << 'EOF'
# This is a comment
port=8080   # inline comment
host=localhost
# Another comment
debug=true
EOF

# Process config
cat app.conf | grep -v '^#' | sed 's/#.*//' | tr -s ' ' | grep -v '^$'
# Output: port=8080
#         host=localhost
#         debug=true

Lab 19: URL Slug Generation

Task: Convert article titles to URL-friendly slugs

Solution
# Article titles
cat > titles.txt << 'EOF'
How to Use Linux Commands
Top 10 Programming Tips!
Learn Python in 30 Days
EOF

# Generate slugs
cat titles.txt | tr 'A-Z' 'a-z' | tr -cs '[:alnum:]' '-' | sed 's/-$//'
# Output: how-to-use-linux-commands
#         top-10-programming-tips-
#         learn-python-in-30-days

Better version:

cat titles.txt | tr 'A-Z' 'a-z' | tr -cs '[:alnum:]' '-' | sed 's/^-//;s/-$//'

Lab 20: DNA Sequence Complement

Task: Generate DNA complement (A↔T, C↔G)

Solution
# DNA sequence
dna="ATCGATCG"

# Generate complement
echo "$dna" | tr 'ATCG' 'TAGC'
# Output: TAGCTAGC

# Generate reverse complement (for DNA analysis)
echo "$dna" | tr 'ATCG' 'TAGC' | rev
# Output: CGATCGAT

Explanation:

  • A → T
  • T → A
  • C → G
  • G → C

Best Practices

1. Use tr for Simple Transformations Only

# GOOD: Simple character replacement
echo "hello" | tr 'a-z' 'A-Z'

# BAD: Use sed instead for patterns
echo "error123" | tr 'error' 'ERROR'  # Wrong approach
sed 's/error/ERROR/' file.txt          # Correct approach

2. Always Pipe to tr

# WRONG: tr doesn't read files directly
tr 'a-z' 'A-Z' file.txt    # Error

# RIGHT: Use cat or input redirection
cat file.txt | tr 'a-z' 'A-Z'
tr 'a-z' 'A-Z' < file.txt

3. Use POSIX Classes for Portability

# Less portable
tr 'a-z' 'A-Z'

# More portable (works across locales)
tr '[:lower:]' '[:upper:]'

4. Combine with Other Tools

# Good pipeline
cat file.txt | tr -s ' ' | tr 'A-Z' 'a-z' | sort | uniq -c

5. Quote Your Arguments

# GOOD
tr 'a-z' 'A-Z'
tr '[:space:]' ' '

# BAD (may cause shell interpretation issues)
tr a-z A-Z

Common Pitfalls

1. SET2 Shorter Than SET1

# If SET2 is shorter, last character repeats
echo "abcdef" | tr 'a-f' 'XY'
# Output: XYYYYY (Y repeats for c, d, e, f)

2. Forgetting Input Source

# Wrong: tr needs input
tr 'a-z' 'A-Z' file.txt    # Error!

# Right: provide input
cat file.txt | tr 'a-z' 'A-Z'
tr 'a-z' 'A-Z' < file.txt

3. Misunderstanding Complement

# Keep only letters
echo "Hello123" | tr -cd '[:alpha:]'
# Output: Hello

# Common mistake: forgetting -d with -c
echo "Hello123" | tr -c '[:alpha:]' ''  # Doesn't work as expected

4. Character Range Order Matters

# Wrong: reverse range
echo "hello" | tr 'z-a' 'A-Z'  # Unexpected results

# Right: ascending order
echo "hello" | tr 'a-z' 'A-Z'

Quick Reference: tr Options

| Option | Description | Example | |--------|-------------|---------| | (none) | Translate SET1 to SET2 | tr 'a-z' 'A-Z' | | -d | Delete characters in SET1 | tr -d '[:digit:]' | | -s | Squeeze repeated chars | tr -s ' ' | | -c | Complement of SET1 | tr -cd '[:alnum:]' | | -t | Truncate SET1 to SET2 length | tr -t 'a-z' 'AB' |


Quick Reference: Character Classes

| Class | Description | Example Characters | |-------|-------------|--------------------| | [:alnum:] | Letters and digits | a-z, A-Z, 0-9 | | [:alpha:] | Letters only | a-z, A-Z | | [:digit:] | Digits | 0-9 | | [:lower:] | Lowercase letters | a-z | | [:upper:] | Uppercase letters | A-Z | | [:space:] | Whitespace | space, tab, newline | | [:punct:] | Punctuation | . , ; : ! ? | | [:blank:] | Space and tab | (space) \t |


Key Takeaways

  1. tr translates or deletes characters from stdin to stdout

  2. Simple and fast for character-by-character transformations

  3. Cannot read files directly - always pipe or redirect input

  4. Case conversion is easy with character ranges or POSIX classes

  5. -d flag deletes specified characters

  6. -s flag squeezes repeated characters to single occurrence

  7. -c flag uses the complement (everything NOT in the set)

  8. POSIX character classes provide portability across systems

  9. Use tr for simple tasks, sed/awk for complex patterns

  10. Perfect for pipelines - combines well with other tools

  11. Quote your arguments to prevent shell interpretation

  12. Test incrementally - build complex transformations step by step


What's Next?

Congratulations! You've mastered the tr command for simple yet powerful text transformations. In the next posts, we'll continue with more text processing topics and then move into SSH and Remote Access, where you'll learn:

  • Understanding SSH protocol and security
  • Connecting to remote Linux servers
  • SSH key-based authentication
  • Using MobaXterm for Windows access
  • File transfer with SCP and WinSCP
  • SSH configuration and best practices

Get ready to manage remote systems like a pro!

Continue your LFCS journey: LFCS Part 39 and beyond


Previous Post: LFCS Part 37: Regular Expressions Part 2 - Advanced


Practice makes perfect! The tr command is simple but incredibly useful. Complete all 20 labs and incorporate tr into your daily text processing workflows. Soon it will become second nature!

Happy transforming! 🚀

Thank you for reading!

Published on December 29, 2025

Owais

Written by Owais

I'm an AIOps Engineer with a passion for AI, Operating Systems, Cloud, and Security—sharing insights that matter in today's tech world.

I completed the UK's Eduqual Level 6 Diploma in AIOps from Al Nafi International College, a globally recognized program that's changing careers worldwide. This diploma is:

  • ✅ Available online in 17+ languages
  • ✅ Includes free student visa guidance for Master's programs in Computer Science fields across the UK, USA, Canada, and more
  • ✅ Comes with job placement support and a 90-day success plan once you land a role
  • ✅ Offers a 1-year internship experience letter while you study—all with no hidden costs

It's not just a diploma—it's a career accelerator.

👉 Start your journey today with a 7-day free trial

Related Articles

Continue exploring with these handpicked articles that complement what you just read

More Reading

One more article you might find interesting