TR(1)
NAME
tr − translate characters
SYNOPSIS
tr [ −cds ] [ string1 [ string2 ] ]
DESCRIPTION
Tr copies the standard input to the standard output with substitution or deletion of selected characters. Input characters found in string1 are mapped into the corresponding characters of string2. Any combination of the options −cds may be used:
−c Complements the set of characters in string1 with respect to the all the characters contained in the current character set.
−d Deletes all input characters in string1.
−s Squeezes all strings of repeated output characters that are in string2 to single characters.
The following abbreviation conventions may be used to introduce ranges of characters or repeated characters into the strings:
[c1−c2] Stands for the ordered string of collating elements c1 to c2, inclusive.
[[:class:]] Stands for the string of characters in type class. Class must be one of alpha, upper, lower, digit, xdigit, alnum, space, print, punct, graph or cntrl. Character classes are expanded in collation order.
[[=c=]] Stands for an equivalence class, i.e., all characters defined as having the same primary collation sequence number as c.
[[.cc.]] Stands for the collating element cc. Multi-character collating elements must be represented in this manner to distinguish them from a list of single-character collating elements.
[a∗n] Stands for n repetitions of a. If the first digit of n is 0, n is considered octal; otherwise, n is taken to be decimal. A zero or missing n is taken to be huge; this facility is useful for padding string2.
The escape character \ may be used as in the shell to remove special meaning from any character in a string. In addition, \ followed by 1, 2, or 3 octal digits stands for the character whose ASCII code is given by those digits.
EXTERNAL INFLUENCES
Environment Variables
LC_COLLATE determines the order of collating elements, the members of equivalence classes, the order in which character classes are expanded, and the identification of multi-character collating elements.
LC_CTYPE determines the interpretation of text as single and/or multi-byte characters, the characters matched by character classes, and the current universe of characters when using the -c option.
If LC_COLLATE or LC_CTYPE is not specified in the environment or is set to the empty string, the value of LANG is used as a default for each unspecified or empty variable. If LANG is not specified or is set to the empty string, a default of "C" (see lang(5)) is used instead of LANG. If any internationalization variable contains an invalid setting, tr behaves as if all internationalization variables are set to "C". See environ(5).
International Code Set Support
Single- and multi-byte character code sets are supported.
EXAMPLES
1. For the ASCII character set and default collation sequence, the following creates a list of all the words in file1 one per line in file2, where a word is taken to be a maximal string of alphabetics. The strings are quoted to protect the special characters from interpretation by the shell; 012 is the ASCII code for newline.
tr −cs "[A−Z][a−z]" "[\012∗]" <file1 >file2
2. The following example does the same as the one above but will work for all character sets and collation sequences.
tr −cs "[[:alpha:]]" "[\012∗]" <file1 >file2
3. The next example may translate all lower-case characters in file1 to upper-case and write the results to standard output.
tr "[[:lower:]]" "[[:upper:]]" <file1
Note that character classes specified in either string1 or string2 are expanded in collation order. As a consequence, this example will not produce the desired effect in locales in which there is not a one to one mapping between lower- and upper-case letters, or in which lower- and upper-case letters collate in a different relative order.
4. 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
5. This example translates instances of the multi-character collating element ch to upper-case. Note that the single characters c and h will not be affected by this operation unless they are part of a ch character sequence.
tr "[[.ch.]]" "[[.CH.]]" <file1 >file2
SEE ALSO
ed(1), sh(1), ascii(5), environ(5), lang(5), regexp(5).
BUGS
Will not handle ASCII NUL in string1 or string2; always deletes NUL from input.
STANDARDS CONFORMANCE
tr: SVID2, XPG2, XPG3
Hewlett-Packard Company — HP-UX Release 7.0: Sept 1989