tr

Basic Usage of tr - Translating and Transforming Text

The tr command is used to translate, replace, remove, or compress characters from standard input. It is commonly used in shell scripting, text processing, log analysis, and automation.

tr [OPTION] SET1 [SET2]

Example:

echo "linux" | tr 'a-z' 'A-Z'

Output:

LINUX
  • tr stands for “translate”
  • Works with standard input
  • Commonly used in pipelines
  • Useful for text transformations and cleanup

Converting Lowercase to Uppercase

To convert lowercase letters into uppercase:

echo "hello world" | tr 'a-z' 'A-Z'

Output:

HELLO WORLD

Breakdown:

  • 'a-z' defines lowercase alphabet range
  • 'A-Z' defines uppercase alphabet range
  • Each character is translated positionally

Examples:

Original Result
a A
b B
z Z

Converting Uppercase to Lowercase

To convert uppercase letters into lowercase:

echo "LINUX ADMIN" | tr 'A-Z' 'a-z'

Output:

linux admin
  • Common for normalization
  • Useful when processing user input or logs

Replacing Specific Characters

To replace one character with another:

echo "2026/05/12" | tr '/' '-'

Output:

2026-05-12
  • Replaces every / with -
  • Useful for formatting data

Removing Characters

To delete characters completely:

echo "hello123" | tr -d '0-9'

Output:

hello

Breakdown:

  • -d means delete characters
  • '0-9' represents all digits

Useful for:

  • cleaning input
  • filtering logs
  • removing unwanted characters

Removing Whitespace

To remove spaces:

echo "Linux Server" | tr -d ' '

Output:

LinuxServer

To remove tabs and spaces:

tr -d ' \t'
  • Common in data cleanup tasks
  • Useful before parsing text

Compressing Repeated Characters

The -s option squeezes repeated characters.

Example:

echo "Linux     Server" | tr -s ' '

Output:

Linux Server
  • -s means squeeze repeats
  • Multiple spaces become single space
  • Useful for formatting messy text

Removing Empty Lines

Example:

cat file.txt | tr -s '\n'
  • Compresses multiple newlines
  • Removes repeated blank lines
  • Useful in report cleanup

Working with Character Classes

The tr command supports predefined character classes.

Convert lowercase to uppercase:

echo "linux" | tr '[:lower:]' '[:upper:]'

Output:

LINUX

Common classes:

Class Meaning
[:lower:] lowercase letters
[:upper:] uppercase letters
[:digit:] numbers
[:alpha:] alphabetic characters
[:space:] whitespace

Removing Digits

To remove all numbers:

echo "server123" | tr -d '[:digit:]'

Output:

server
  • Removes all numeric characters
  • Useful for sanitizing text

Keeping Only Specific Characters

To remove everything except letters:

echo "linux123server!" | tr -cd '[:alpha:]'

Output:

linuxserver

Breakdown:

  • -c complements selection
  • -d deletes matched characters
  • Keeps only alphabetic characters

Using tr with Pipes

The tr command is heavily used in pipelines.

Convert usernames to uppercase:

cut -d ":" -f1 /etc/passwd | tr 'a-z' 'A-Z'

Remove spaces from output:

df -h | tr -s ' '

Normalize input:

cat users.txt | tr '[:upper:]' '[:lower:]'
  • tr works best together with pipes
  • Important for automation workflows

Combining Multiple Options

Example:

tr -ds '0-9' ' '

Breakdown:

  • -d deletes digits
  • -s squeezes repeated spaces

Another example:

tr -cd '[:alpha:]\n'

This:

  • removes everything except letters and newlines
  • useful for text sanitization

Common Administrative Examples

Convert log data to lowercase:

cat access.log | tr 'A-Z' 'a-z'

Remove empty spaces:

cat config.txt | tr -s ' '

Strip numbers from usernames:

echo "admin123" | tr -d '0-9'

Normalize CSV formatting:

cat users.csv | tr ';' ','

Practical Script Example (Step-by-Step Explanation)

Script

#!/bin/bash

FILE="/var/log/access.log"

echo "Normalized log output:"

cat $FILE | tr 'A-Z' 'a-z' | tr -s ' '

Step 1: Shebang

#!/bin/bash
  • Defines Bash interpreter
  • Ensures script executes consistently

Step 2: Defining file variable

FILE="/var/log/access.log"
  • Stores log file location
  • Makes script easier to modify

Example value:

/var/log/access.log

Step 3: Printing informational message

echo "Normalized log output:"
  • Displays readable heading
  • Organizes script output

Step 4: Reading log file

cat $FILE
  • Reads file content
  • Sends output into pipeline

Step 5: Converting uppercase to lowercase

tr 'A-Z' 'a-z'
  • Converts all uppercase letters
  • Normalizes inconsistent log formatting

Example:

ERROR

becomes:

error

Step 6: Compressing repeated spaces

tr -s ' '
  • Replaces multiple spaces with single space
  • Cleans messy formatting

Example:

user     login

becomes:

user login

What this script does

Step-by-step flow:

  1. Reads log file
  2. Converts text to lowercase
  3. Compresses repeated spaces
  4. Produces normalized output

Why this matters in production

This type of processing is useful for:

  • log normalization
  • preprocessing data
  • cleaning inconsistent formatting
  • preparing text for parsing
  • automation pipelines

The tr command is widely used in:

  • DevOps workflows
  • shell scripting
  • Linux administration
  • data sanitization

Common Beginner Mistakes

Trying to use tr directly on files:

Incorrect:

tr 'a-z' 'A-Z' file.txt

Correct usage:

cat file.txt | tr 'a-z' 'A-Z'

Another mistake:

Using mismatched ranges:

tr 'abc' 'XY'

This creates inconsistent mappings.

Correct approach:

tr 'abc' 'XYZ'

Another mistake:

Expecting tr to process words instead of characters.

tr operates character-by-character, not word-by-word.


Summary

In this guide, you learned:

  • how to translate characters using tr
  • converting uppercase and lowercase text
  • deleting characters
  • squeezing repeated characters
  • using character classes
  • sanitizing input
  • combining tr with pipes
  • practical shell scripting with tr

These skills are essential for:

  • Linux administration
  • shell scripting
  • text normalization
  • automation
  • data cleanup

Additional tr parameters and concepts not covered in this guide include:

POSIX locale handling
octal character notation
hexadecimal escape sequences
multibyte character limitations
Unicode handling considerations
--help: Display help information
--version: Display version information