Linux and Unix tr command

Quick links

About tr
Syntax
Examples
Related commands
Linux and Unix main page

About tr

The tr command automatically translates (substitutes) sets of characters.

Syntax

tr [-Ccsu] string1 string2

In this form, the characters in the string string1 are translated into the characters in string2 where the first character in string1 is translated into the first character in string2 and so on. If string1 is longer than string2, the last character found in string2 is duplicated until string1 is exhausted.

tr [-Ccu] -d string1

In this form, the characters in string1 are deleted from the input.

tr [-Ccu] -s string1

In this form, the characters in string1 are compressed as described for the -s option (see below).

tr [-Ccu] -ds string1 string2

In the fourth form, the characters in string1 are deleted from the input, and the characters in string2 are compressed as described for the -s option.

Description

The tr utility copies the standard input to the standard output with substitution or deletion of selected characters.

Options

-C Complement the set of characters in string1, that is "-C ab" includes every character except for 'a' and 'b'.
-c Same as -C but complement the set of values in string1.
-d Delete characters in string1 from the input.
-s Squeeze multiple occurrences of the characters listed in the last operand (either string1 or string2) in the input into a single instance of the character. This occurs after all deletion and translation is completed.
-u Guarantee that any output is unbuffered.

How Characters Are Specified

When specifying the characters to translate with tr, the following conventions are used to represent sets (or "classes") of characters.

Any character not described by one of the following conventions simply represents itself.

\octal A backslash followed by 1, 2 or 3 octal digits represents a character with that encoded value. To follow an octal sequence with a digit as a character, pad the octal sequence on the left with zeroes.
\character A backslash followed by certain special characters maps to special values:

\a The "alert" character, which issues a notification or alert to the terminal.
\b Backspace.
\f Form feed.
\n Newline.
\r Carriage return.
\t Tab.
\v Vertical tab.

A backslash followed by any other character maps to that character.
c-c Character range. For non-octal range endpoints represents the range of characters between the range endpoints, inclusive and in ascending order, as defined by the collation sequence. If either or both of the range endpoints are octal sequences, it represents the range of specific coded values between the range endpoints, inclusive.
[:class:] Represents all characters belonging to the defined character class. Class names are:
alnum Alphanumeric characters.
alpha Alphabetic characters.
blank White space characters.
cntrl Control characters.
digit Numeric characters.
graph Graphic characters.
ideogram Ideographic characters.
lower Lowercase alphabetic characters.
phonogram Phonographic characters.
print Printable characters.
punct Punctuation characters.
rune Valid characters.
space Space characters.
special Special characters.
upper Uppercase alphabetic characters.
xdigit Hexadecimal characters.

When "[:lower:]" appears in string1 and "[:upper:]" appears in the same relative position in string2, it represents the characters pairs from the toupper mapping in the LC_CTYPE category of the current locale. When "[:upper:]" appears in string1 and "[:lower:]" appears in the same relative position in string2, it represents the characters pairs from the tolower mapping in the LC_CTYPE category of the current locale.

With the exception of case conversion, characters in the classes are in unspecified order.

For specific information as to which ASCII characters are included in these classes, see ctype and related manual pages.

"[=equiv=]" Represents all characters belonging to the same equivalence class as equiv, ordered by their encoded values.
[#*n] Represents n repeated occurrences of the character represented by #. This expression is only valid when it occurs in string2. If n is omitted, or is zero, it is be interpreted as large enough to extend string2 sequence to the length of string1. If n has a leading zero, it is interpreted as an octal value, otherwise, it is interpreted as a decimal value.

Environment

The LANG, LC_ALL, LC_CTYPE and LC_COLLATE environment variables affect the execution of tr.

Exit Status

tr returns an exit status of 0 if it operated successfully, and a value greater than zero if an error occurred.

Examples

tr -cs "[:alpha:]" "\n" < file1

Create a list of the words in file1, one per line, where a word is taken to be a maximal string of letters.

tr "[:lower:]" "[:upper:]" < file1

Translate the contents of file1 to uppercase.

tr -cd "[:print:]" < file1

Remove all non-printable characters from file1.

tr "[=e=]" "e"

Remove all "diacritical" marks from accented versions of the letter e.

Related commands

ed
sed
sh