TR(1) | User Commands | TR(1) |
tr - translate characters
/usr/bin/tr [-cds] [string1 [string2]]
/usr/xpg4/bin/tr [-cs] string1 string2
/usr/xpg4/bin/tr -s | -d [-c] string1
/usr/xpg4/bin/tr -ds [-c] string1 string2
/usr/xpg6/bin/tr [-c | -C] [-s] string1 string2
/usr/xpg6/bin/tr -s [-c | -C] string1
/usr/xpg6/bin/tr -d [-c | -C] string1
/usr/xpg6/bin/tr -ds [-c | -C] string1 string2
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.
The following options are supported:
-c
-C
-d
-s
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 is 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 contains 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 contains only those characters defined as the second characters in each of the toupper or tolower character pairs, as appropriate. (See toupper(3C) and tolower(3C)).
An empty string used for string1 or string2 produces undefined results.
The following operands are supported:
string1
string2
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 excludes, without a diagnostic, those multi-character elements from the resulting array.
character
\octal
\character
c-c
[c-c]
If either or both of the range endpoints are octal sequences of the form \octal, represents the range of specific coded binary values between two range endpoints, inclusively.
[:class:]
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 are 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 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 contain the characters from the tolower mapping in the LC_CTYPE category of the current locale. The first character from each mapping pair is in the array for string1 and the second character from each mapping pair is 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 an unspecified order.
If the name specified for class does not define a valid character class in the current locale, the behavior is undefined.
[=equiv=]
[x*n]
If n is omitted or is 0, /usr/bin/tr interprets this as huge; /usr/xpg4/bin/tr and /usr/xpg6/bin/tr interprets this as large enough to extend the string2-based sequence to the length of the string1-based sequence.
See largefile(7) for the description of the behavior of tr when encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
Example 1 Creating a list of words
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
Example 2 Translating characters
This example translates all lower-case characters in file1 to upper-case and writes the results to standard output.
tr "[:lower:]" "[:upper:]" <file1
Notice that the caveat expressed in the corresponding example in XPG3 is no longer in effect. 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).
Example 3 Identifying equivalent characters
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
See environ(7) for descriptions of the following environment variables that affect the execution of tr: LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.
The following exit values are returned:
0
>0
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
CSI | Enabled |
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
CSI | Enabled |
Interface Stability | Committed |
Standard | See standards(7). |
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
CSI | Enabled |
Interface Stability | Committed |
Standard | See standards(7). |
ed(1), sed(1), sh(1), tolower(3C), toupper(3C), ascii(7), attributes(7), environ(7), largefile(7), regex(7), standards(7)
Unlike some previous versions, /usr/xpg4/bin/tr correctly processes NUL characters in its input stream. NUL characters can be stripped by using tr -d '\000'.
June 13, 2021 | OmniOS |