tr

Translate characters (POSIX)

Syntax:

tr [-cs] [-r filename] string1 string2

tr [-cs] [-r filename] string1

tr -d [-c] [-r filename] string1

tr -ds [-c] [-r filename] string1 string2

Runs on:

QNX Neutrino, Microsoft Windows

Options:

-c
Complement the set of characters in string1 with respect to the universe of characters from 00 through FF hex. Characters in string1 are copied unchanged while all other characters are translated.
-d
Delete all input characters in string1 (or not in string1 for -dc).
-r filename
(QNX Neutrino extension) Translate the named file in place (don't use stdin or stdout).
-s
Squeeze all output strings of one or more instances of a single character in string1 to a single instance of the corresponding character in string2.

If you don't specify string2, tr squeezes instances of the characters in string1 to a single instance of that character.

string1
Translation character string (translate from).
string2
Translation character string (translate to).
Note: If you specify both -d and -s, tr deletes instances of the characters in string1 and squeezes instances of the characters in string2 (i.e., tr doesn't translate in this case).

Description:

The tr utility copies the standard input to the standard output with substitution or deletion of selected characters. The options specified and the string1 and string2 operands control translations that occur while copying characters.

The default behavior is to replace each input character found in string1 with the character at the same position in string2, while copying characters not in string1 unchanged.

When string2 is shorter than string1, string2 is extended to the length of string1 by duplicating the last character of string2. If string2 is explicitly a string of zero length, it's padded with NUL characters.

Note: The string1 and string2 operands often require quoting to avoid interpretation by the shell. Single quotes are usually the proper quoting mechanism.

Conventions for string1 and string2

Use the following conventions in string1 or string2 or both to specify characters, character ranges, character classes, or collating elements:

character
Represents that character.
\octal
A backslash followed by 1, 2, or 3 octal digits represents a character with that encoded value.
\character
A backslash followed by any character except an octal digit represents that character.
c-c
Represents the range of characters between the range endpoints, inclusive.
[c-c]
(QNX Neutrino extension) The System V method of representing a range of characters.
[:class:]
Represents all characters belonging to the defined character class. Allowable names for class are:

alnum, alpha, blank, cntrl, digit, graph, lower, print, punct, space, upper, xdigit

[.cs.]
Represents a collating symbol. Multicharacter collating symbols must be represented as collating symbols to distinguish them from a string of the same characters. This implementation allows an arbitrary string to be treated as a collating symbol (QNX Neutrino extension).
[x*n]
Represents n repeated occurrences of the character or collating symbol x. This expression is valid only in string2. If n is omitted or is zero, it's interpreted as large enough to extend the string2-based sequence to the length of the string1-based sequence. If n has a leading zero, it's interpreted as an octal value. Otherwise, it's interpreted as a decimal value.

Examples:

Convert all lowercase characters in the input to the corresponding uppercase characters:

tr '[:lower:]' '[:upper:]' <file1 >file2

Or:

tr '[a-z]' '[A-Z]' <file1 >file2

Create a list of all words in file1 one per line in file2 where a word is taken to be a maximal string of letters (octal 012 is the code for newline):

tr -cs '[:alpha:]' '[\012*]' <file 1 >file2

Convert a DOS file into a UNIX file:

tr -d '\15' <infile >outfile

Exit status:

0
Success
1
An error occurred.