tr(1)


tr -- translate characters

Synopsis

tr [-cs] string1 string2

tr -s [-c] string1

tr -d [-c] string1

tr -ds [-c] string1 string2

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 and single-character collating elements. Searches and translations are performed on characters, not bytes.

Options


-c
Complement the set of characters specified by string1 with respect to the universe of characters whose ASCII codes are 001 through 377 octal. See ``Usage''.

-d
Delete all occurrences of input characters that are specified by string1.

-s
Replace instances of repeated characters in string2 with a single character as described in ``Usage''.

The following operands are supported:


string1 string2
Translation control strings. Each string represents a set of characters to be converted into an array of characters used for the translation. For a detailed description of how the strings are interpreted, see ``Usage''.

The following environment variables affect the execution of tr:


LANG
Provide a default value for internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL
If set to a non-empty string value, override the values of all the other internationalization variables.

LC_COLLATE
Determine the locale for the behavior of range expressions and equivalence classes.

LC_CTYPE
Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- versus multi-byte characters in arguments) and the behavior of character classes.

LC_MESSAGES
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

Usage

The operands string1 and string2 (if specified) define two arrays of characters. The constructs in the following list can be used to specify characters or single-character collating elements. If any of the constructs result in multi-character collating elements, tr will exclude, without a diagnostic, those multi-character elements from the resulting array.

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

\octal
An octal consists of a backslash (\) followed by a sequence of one, two, or three octal digits (that is, the numerals 0, 1, 2, 3, 4, 5, 6, and 7). The sequence causes the character whose ASCII encoding is represented by the one-, two- or three-digit octal integer to be placed into the array.

\character
The backslash-escape sequences \\, \a, \b, \f, \n, \r, \t, \v are supported. Any other character following the backslash (other than an octal digit) represents itself.

c-c
Represents the range of collating elements between the range endpoints, inclusive, as defined by the current setting of the LC_COLLATE locale category. The starting endpoint must precede the second endpoint in the current collation order. The characters or collating elements in the range are placed in the array in ascending collation sequence. If either endpoint is specified as an octal sequence, the range is based on the encoded values of the endpoints, not the current collation order. For example, [a-z] stands for the string of characters whose ASCII codes run from character ``a'' to character ``z'', inclusive.

[:class:]
Represents all characters belonging to the defined character class, as defined by the current setting of the LC_CTYPE locale category. The following character class names will be accepted when specified in string1:
   alnum blank digit lower punct upper
   alpha cntrl graph print space xdigit
In addition, character class expressions of the form [:name:] are recognized in those locales where the name keyword has been given a ``charclass'' definition in the LC_CTYPE category.

When both the -d and -s options are specified, any of the character class names will be accepted in string2.

Otherwise, only character class names lower or upper are valid in string2 and then only if the corresponding character class (upper and lower, respectively) is specified in the same relative position in string1. Such a specification is interpreted as a request for case conversion. When [:lower:] appears in string1 and [:upper:] appears in string2, the arrays will contain the characters from the toupper mapping in the LC_CTYPE category of the current locale. When [:upper:] appears in string1 and [:lower:] appears in string2, the arrays will contain the characters from the tolower mapping in the LC_CTYPE category of the current locale. The first character from each mapping pair will be in the array for string1 and the second character from each mapping pair will be in the array for string2 in the same relative position.

Except for case conversion, the characters specified by a character class expression are placed in the array in a random order.

If the name specified for class does not define a valid character class in the current locale, an error occurs.


[=equiv=]
Represents all characters or collating elements belonging to the same equivalence class as equiv, as defined by the current setting of the LC_COLLATE locale category. An equivalence class expression is allowed only in string1, or in string2 when it is being used by the combined -d and -s options. The characters belonging to the equivalence class are placed in the array in an unspecified order.

[x*n]
Represents n repeated occurrences of the character x. Because this expression is used to map multiple characters to one, it is only valid when it occurs 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.

When the -d option is not specified:

When the -d option is specified:

When the -s option is specified, after any deletions or translations have taken place, repeated sequences of the same character will be replaced by one occurrence of the same character, if the character is found in the array specified by the last operand. If the last operand contains a character class, such as the following example:

tr -s '[:space:]'

the last operand's array will contain all of the characters in that character class. However, in a case conversion, as described previously, such as:

tr -s '[:upper:]' '[:lower:]'

the last operand's array will contain only those characters defined as the second characters in each of the ``toupper'' or ``tolower'' character pairs, as appropriate.

An empty string used for string1 or string2 produces results as described under ``Warnings''.

Examples

The following example creates a list of all words in file1 one per line in file2, where a word is taken to be a maximal string of letters.

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

The next example translates all lower-case characters in file1 to upper-case and writes the results to standard output.

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

This case conversion is now a special case that employs the ``tolower'' and ``toupper'' classifications, ensuring that proper mapping is accomplished (when the locale is correctly defined).

This example uses an equivalence class to identify accented variants of the base character ``e'' in file1, which are stripped of diacritical marks and written to file2:

tr "[=e=]" e <file1 >file2

SH "Warnings" If necessary, string1 and string2 can be quoted to avoid pattern matching by the shell.

If an ordinary digit (representing itself) is to follow an octal sequence, the octal sequence must use the full three digits to avoid ambiguity.

It should be noted that, despite similarities in appearance, the string operands used by tr are not regular expressions.

Unlike some earlier versions of this command, tr correctly processes NULL characters in its input stream. NULL characters can be stripped by using:

tr -d ' 00'

When string2 is shorter than string1, The command will pad string2 with the last character found in string2. Thus, it is possible to do the following:

tr 0123456789 d

which would translate all digits to the letter ``d''. This is different from the traditional System V behavior, which does not pad string2. A portable application cannot rely on this padding behavior, and so it would have to code the previous example in the following way:

tr 0123456789 '[d*]'

The tr command now requires the dash (-) character to be escaped with a backslash.

References

ed(1), sed(1), ascii(5)


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004