tr

text processinglinux
The tr command is one of the most frequently used commands in Linux/Unix-like operating systems. tr The tr command (translate) is used to translate, squeeze, and/or delete characters from standard input, writing to standard output. It's a simple yet powerful tool for character-level text transformations.

Quick Reference

Command Name:

tr

Category:

text processing

Platform:

linux

Basic Usage:

tr [options] [arguments]

Common Use Cases

  • 1

    Text translation

    Translate or delete characters in text data

  • 2

    Text processing

    Manipulate text data in pipelines and scripts

  • 3

    Data cleaning

    Clean and sanitize text data

  • 4

    Scripting

    Use in shell scripts to process text data programmatically

Syntax

tr [OPTION]... SET1 [SET2]

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 a repeated character in SET1 with a single occurrence of that character
-t, --truncate-set1 First truncate SET1 to length of SET2
--help Display a help message and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the tr command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

echo "Hello World" | tr 'a-z' 'A-Z'

Converts lowercase letters to uppercase. This translates every character in the first set (lowercase a-z) to the corresponding character in the second set (uppercase A-Z).

echo "Hello World" | tr -d ' '

Deletes all spaces from the input. The -d option tells tr to delete any characters that are in the specified set, in this case, a space character.

echo "Hello   World" | tr -s ' '

Squeezes multiple spaces into a single space. The -s option replaces repeated occurrences of a character in the input with a single occurrence of that character.

cat file.txt | tr '\n' ' '

Replaces newlines with spaces, effectively converting a multi-line file to a single line. This is useful for joining lines in a text file.

Advanced Examples:

echo "Hello 123 World 456" | tr -d '[:digit:]'

Deletes all digits from the input. This uses POSIX character classes, which are predefined sets of characters. [:digit:] represents all numeric digits.

echo "hello world" | tr '[:lower:]' '[:upper:]'

Converts lowercase letters to uppercase using POSIX character classes. This is similar to the first example but uses the predefined classes which makes it more readable and portable.

echo "hello\nworld" | tr -s '[:space:]'

Squeezes all whitespace characters (spaces, tabs, newlines). The [:space:] character class includes all types of whitespace characters.

echo "hello123world456" | tr -c '[:digit:]' ' '

Replaces all non-digit characters with spaces. The -c option complements the set of characters, meaning it operates on characters not in the specified set.

echo "hello" | tr 'helo' '1234'

Maps individual characters to new characters. In this example, 'h' becomes '1', 'e' becomes '2', and so on. This is useful for simple character-by-character substitution.

echo "hello" | tr -cd '[:alnum:]\n'

Deletes all characters except alphanumeric characters and newlines. This combines the -c and -d options to remove everything except the specified characters.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Character Sets in tr:

  • Individual characters can be specified directly: tr 'abc' 'xyz'
  • Ranges can be specified with a hyphen: tr 'a-z' 'A-Z'
  • Character classes use the syntax [:class:]
  • Special sequences: \\ (backslash), \a (alert), \b (backspace), \f (form feed), \n (newline), \r (carriage return), \t (horizontal tab), \v (vertical tab)

POSIX Character Classes:

  • [:alnum:] - Alphanumeric characters [a-zA-Z0-9]
  • [:alpha:] - Alphabetic characters [a-zA-Z]
  • [:blank:] - Blank characters (space and tab)
  • [:cntrl:] - Control characters
  • [:digit:] - Numeric characters [0-9]
  • [:graph:] - Graphic characters (visible characters)
  • [:lower:] - Lowercase letters [a-z]
  • [:print:] - Printable characters
  • [:punct:] - Punctuation characters
  • [:space:] - Whitespace characters (space, tab, newline, etc.)
  • [:upper:] - Uppercase letters [A-Z]
  • [:xdigit:] - Hexadecimal digits [0-9a-fA-F]

Translation Rules:

  • When translating, each character in SET1 is replaced by the corresponding character in SET2
  • If SET1 is longer than SET2, the extra characters in SET1 are mapped to the last character in SET2
  • If SET1 is shorter than SET2, the extra characters in SET2 are ignored
  • When using -d, SET2 is not used
  • When using -s without -d, repeated characters in SET1 are replaced by a single occurrence of that character
  • When using -s with -d, sequences of characters that were deleted are squeezed down to a single occurrence of that character

Common Use Cases:

  • Converting case (uppercase to lowercase or vice versa)
  • Removing specific characters from text
  • Converting spaces to tabs or vice versa
  • Removing non-printable characters or control characters
  • Basic text normalization (removing excess whitespace)
  • Simple data cleaning and formatting
  • Creating one-line records from multi-line input

Limitations:

  • tr processes character-by-character, not pattern-by-pattern like sed or awk
  • tr cannot perform complex translations based on context
  • tr can only read from standard input, not directly from files
  • tr works best for simple character replacements or deletions

Related Commands:

  • sed - Stream editor for more complex text transformations
  • awk - Pattern scanning and text processing language
  • grep - Search for patterns in text
  • cut - Remove sections from each line of files
  • col - Filter reverse line feeds from input

Tips & Tricks

1

Use the -d option to delete characters

2

Use the -s option to squeeze repeated characters

3

Use the -c option to complement the set of characters

4

Use the -t option to translate characters

5

Use the --help option to display help

Common Use Cases

Text translation

Translate or delete characters in text data

Text processing

Manipulate text data in pipelines and scripts

Data cleaning

Clean and sanitize text data

Scripting

Use in shell scripts to process text data programmatically

Character set conversion

Convert between different character sets

Related Commands

These commands are frequently used alongside tr or serve similar purposes:

Use Cases

1

Text translation

Translate or delete characters in text data

2

Text processing

Manipulate text data in pipelines and scripts

3

Data cleaning

Clean and sanitize text data

4

Scripting

Use in shell scripts to process text data programmatically

5

Character set conversion

Convert between different character sets

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the tr command works in different scenarios.

$ tr
View All Commands