tr [−c|−C] [−s] string1 string2
tr −s [−c|−C] string1
tr −d [−c|−C] string1
tr −ds [−c|−C] string1 string2
- Complement the set of values specified by string1. See the EXTENDED DESCRIPTION section.
- Complement the set of characters specified by string1. See the EXTENDED DESCRIPTION section.
- Delete all occurrences of input characters that are specified by string1.
- Replace instances of repeated characters with a single character, as described in the EXTENDED DESCRIPTION section.
- string1, string2
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1‐2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
- If set to a non-empty string value, override the values of all the other internationalization variables.
- Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments) and the behavior of character classes.
- Determine the location of message catalogs for the processing of LC_MESSAGES.
- Any character not described by one of the conventions below shall represent itself.
- Octal sequences can be used to represent characters with specific coded values. An octal sequence shall consist of a <backslash> followed by the longest sequence of one, two, or three-octal-digit characters (01234567). The sequence shall cause the value whose encoding is represented by the one, two, or three-digit octal integer to be placed into the array. Multi-byte characters require multiple, concatenated escape sequences of this type, including the leading <backslash> for each byte.
- The <backslash>-escape sequences in the Base Definitions volume of POSIX.1‐2008, Table 5-1, Escape Sequences and Associated Actions ('\\', '\a', '\b', '\f', '\n', '\r', '\t', '\v') shall be supported. The results of using any other character, other than an octal digit, following the <backslash> are unspecified. Also, if there is no character following the <backslash>, the results are unspecified.
- In the POSIX locale, this construct shall represent the range of collating elements between the range endpoints (as long as neither endpoint is an octal sequence of the form \ octal), inclusive, as defined by the collation sequence. The characters or collating elements in the range shall be placed in the array in ascending collation sequence. If the second endpoint precedes the starting endpoint in the collation sequence, it is unspecified whether the range of collating elements is empty, or this construct is treated as invalid. In locales other than the POSIX locale, this construct has unspecified behavior.
If either or both of the range endpoints are octal sequences of the form \ octal, this shall represent the range of specific coded values between the two range endpoints, inclusive.
- 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 shall be accepted when specified in string1:
alnum blank digit lower punct upper alpha cntrl graph space xdigit
In addition, character class expressions of the form [:name:] shall be 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 shall 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 shall be interpreted as a request for case conversion. When [:lower:] appears in string1 and [:upper:] appears in string2, the arrays shall 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 shall contain the characters from the tolower mapping in the LC_CTYPE category of the current locale. The first character from each mapping pair shall be in the array for string1 and the second character from each mapping pair shall be in the array for string2 in the same relative position. Except for case conversion, the characters specified by a character class expression shall be placed in the array in an unspecified order. If the name specified for class does not define a valid character class in the current locale, the behavior is undefined.
- 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 shall be 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 shall be placed in the array in an unspecified order.
- 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 shall be 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 shall be interpreted as an octal value. Otherwise, it shall be interpreted as a decimal value.
- If string2 is present, each input character found in the array specified by string1 shall be replaced by the character in the same relative position in the array specified by string2. If the array specified by string2 is shorter that the one specified by string1, or if a character occurs more than once in string1, the results are unspecified.
- If the −C option is specified, the complements of the characters specified by string1 (the set of all characters in the current character set, as defined by the current setting of LC_CTYPE, except for those actually specified in the string1 operand) shall be placed in the array in ascending collation sequence, as defined by the current setting of LC_COLLATE.
- If the −c option is specified, the complement of the values specified by string1 shall be placed in the array in ascending order by binary value.
- Because the order in which characters specified by character class expressions or equivalence class expressions is undefined, such expressions should only be used if the intent is to map several characters into one. An exception is case conversion, as described previously.
- Input characters found in the array specified by string1 shall be deleted.
- When the −C option is specified with −d, all characters except those specified by string1 shall be deleted. The contents of string2 are ignored, unless the −s option is also specified.
- When the −c option is specified with −d, all values except those specified by string1 shall be deleted. The contents of string2 shall be ignored, unless the −s option is also specified.
- The same string cannot be used for both the −d and the −s option; when both options are specified, both string1 (used for deletion) and string2 (used for squeezing) shall be required.
tr −s '[:space:]'
tr −s '[:upper:]' '[:lower:]'
- All input was processed successfully.
- An error occurred.
tr 0123456789 d
tr 0123456789 '[d*]'
tr −d '\000'
- 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 lowercase characters in file1 to uppercase and writes the results to standard output.
tr "[:lower:]" "[:upper:]" <file1
- 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
tr '[a-z]' '[A-Z]'
tr a-z A-Z