tr Command
Translate, delete, or squeeze characters from standard input
Syntax:
tr [OPTION]... SET1 [SET2]
Description
The tr command is used to translate, delete, or squeeze characters from standard input and write the result to standard output. It's a powerful text processing tool that can perform character-by-character transformations on text streams.
Note: The tr command only reads from standard input and writes to standard output. It cannot read files directly - use input redirection or pipes to process file contents.
Common Options
| Option | Description |
|---|---|
-c, --complement |
Use the complement of SET1 |
-d, --delete |
Delete characters in SET1, do not translate |
-s, --squeeze-repeats |
Replace each sequence of repeated characters in SET1 with a single occurrence |
-t, --truncate-set1 |
First truncate SET1 to length of SET2 |
--help |
Display help message and exit |
--version |
Output version information and exit |
Character Sets
The tr command supports various character set notations:
| Notation | Description | Example |
|---|---|---|
[:alnum:] |
All alphanumeric characters | a-z, A-Z, 0-9 |
[:alpha:] |
All alphabetic characters | a-z, A-Z |
[:digit:] |
All digits | 0-9 |
[:lower:] |
All lowercase letters | a-z |
[:upper:] |
All uppercase letters | A-Z |
[:space:] |
All whitespace characters | space, tab, newline |
[:punct:] |
All punctuation characters | !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ |
a-z |
Range of characters | All lowercase letters |
\n |
Newline character | Line break |
\t |
Tab character | Horizontal tab |
Examples
Convert lowercase to uppercase:
echo "hello world" | tr '[:lower:]' '[:upper:]' # Output: HELLO WORLD echo "hello world" | tr 'a-z' 'A-Z' # Output: HELLO WORLD
Convert uppercase to lowercase:
echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]' # Output: hello world echo "HELLO WORLD" | tr 'A-Z' 'a-z' # Output: hello world
Delete specific characters:
echo "hello123world456" | tr -d '[:digit:]' # Output: helloworld echo "hello world!" | tr -d ' !' # Output: helloworld
Replace spaces with underscores:
echo "hello world test" | tr ' ' '_' # Output: hello_world_test echo "multiple spaces" | tr -s ' ' '_' # Output: multiple_spaces
Squeeze repeated characters:
echo "hellooo world" | tr -s 'o ' # Output: helo world echo "aabbccdd" | tr -s 'a-z' # Output: abcd
Replace newlines with spaces:
cat file.txt | tr '\n' ' ' # Converts all newlines to spaces printf "line1\nline2\nline3" | tr '\n' ' ' # Output: line1 line2 line3
Remove all punctuation:
echo "Hello, World! How are you?" | tr -d '[:punct:]' # Output: Hello World How are you echo "[email protected]" | tr -d '@.' # Output: testexamplecom
Convert tabs to spaces:
cat file.txt | tr '\t' ' ' # Converts all tabs to spaces printf "col1\tcol2\tcol3" | tr '\t' ',' # Output: col1,col2,col3
Use complement to keep only specific characters:
echo "abc123def456" | tr -cd '[:digit:]' # Output: 123456 (keeps only digits) echo "Hello123World456" | tr -cd '[:alpha:]' # Output: HelloWorld (keeps only letters)
ROT13 encoding:
echo "Hello World" | tr 'A-Za-z' 'N-ZA-Mn-za-m' # Output: Uryyb Jbeyq echo "Uryyb Jbeyq" | tr 'A-Za-z' 'N-ZA-Mn-za-m' # Output: Hello World (decoding)
Advanced Usage
Process file contents:
# Convert file to uppercase tr '[:lower:]' '[:upper:]' < input.txt > output.txt # Remove all digits from a file tr -d '[:digit:]' < input.txt > output.txt # Replace multiple spaces with single space tr -s ' ' < input.txt > output.txt
Chain with other commands:
# Count unique words (case-insensitive) cat file.txt | tr '[:upper:]' '[:lower:]' | tr -s '[:space:]' '\n' | sort | uniq -c # Extract only alphanumeric characters cat file.txt | tr -cd '[:alnum:]\n' # Convert CSV to tab-separated cat file.csv | tr ',' '\t'
Text cleaning:
# Remove control characters cat file.txt | tr -d '[:cntrl:]' # Normalize whitespace cat file.txt | tr -s '[:space:]' ' ' # Remove non-printable characters cat file.txt | tr -cd '[:print:]\n'
Common Use Cases
- Case conversion: Converting text between uppercase and lowercase
- Character replacement: Replacing specific characters or character sets
- Text cleaning: Removing unwanted characters from text
- Format conversion: Converting between different text formats
- Data preprocessing: Preparing text data for further processing
- Character encoding: Simple character substitution ciphers
- Whitespace normalization: Standardizing spacing in text
- File format conversion: Converting between CSV, TSV, and other formats
Tips and Best Practices
- Use character classes like
[:lower:]instead of ranges likea-zfor better portability - The
-soption is useful for normalizing repeated characters - Combine
-cand-dto keep only specific character types - Use
trwith pipes to process command output - Remember that
trworks character-by-character, not on words or patterns - Test your tr commands with small samples before processing large files
- Use input/output redirection to process files efficiently
- Escape special characters in the shell when necessary