[Previous] [Contents] [Next]

tr

Translate characters (POSIX)

Syntax:

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

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

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 extension) Translate the named file in place (don't use stdin/stdout).
-s
Squeeze all output strings of one or more instances of a single character in string2 to a single instance of that character.
string1
Translation character string (translate from).
string2
Translation character string (translate to).

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

The following conventions can be used 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.
[:class:]
Represents all characters belonging to the defined character class. Allowable names for class are:
alpha upper lower digit xdigit alnum
space punct print graph cntrl blank
[.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 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 is 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 is interpreted as an octal value. Otherwise, it is interpreted as a decimal value.

Examples:

Converts 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 QNX file:

    tr -d '\15' <infile >outfile

Exit status:

0
All input was processed successfully.
1
An error occurred.

See also:

awk, sed, textto


[Previous] [Contents] [Next]