Linux tr command

Updated: 11/06/2021 by Computer Hope
tr command

On Unix-like operating systems, the tr command automatically translates (substitutes, or maps) one set of characters to another.

This page covers the GNU/Linux version of tr.

Description

The tr utility copies the standard input to the standard output with substitution or deletion of selected 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.

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 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.

Except for case conversion, characters in the classes are in unspecified order.

For specific information for what 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.

ed — A simple text editor.
sed — A utility for filtering and transforming text.
sh — The Bourne shell command interpreter.