.\" .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for .\" permission to reproduce portions of its copyrighted documentation. .\" Original documentation from The Open Group can be obtained online at .\" http://www.opengroup.org/bookstore/. .\" .\" The Institute of Electrical and Electronics Engineers and The Open .\" Group, have given us permission to reprint portions of their .\" documentation. .\" .\" In the following statement, the phrase ``this text'' refers to portions .\" of the system documentation. .\" .\" Portions of this text are reprinted and reproduced in electronic form .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition, .\" Standard for Information Technology -- Portable Operating System .\" Interface (POSIX), The Open Group Base Specifications Issue 6, .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics .\" Engineers, Inc and The Open Group. In the event of any discrepancy .\" between these versions and the original IEEE and The Open Group .\" Standard, the original IEEE and The Open Group Standard is the referee .\" document. The original Standard can be obtained online at .\" http://www.opengroup.org/unix/online.html. .\" .\" This notice shall appear on any product containing this material. .\" .\" The contents of this file are subject to the terms of the .\" Common Development and Distribution License (the "License"). .\" You may not use this file except in compliance with the License. .\" .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE .\" or http://www.opensolaris.org/os/licensing. .\" See the License for the specific language governing permissions .\" and limitations under the License. .\" .\" When distributing Covered Code, include this CDDL HEADER in each .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. .\" If applicable, add the following below this CDDL HEADER, with the .\" fields enclosed by brackets "[]" replaced with your own identifying .\" information: Portions Copyright [yyyy] [name of copyright owner] .\" .\" .\" Copyright 1989 AT&T .\" Copyright (c) 1992, X/Open Company Limited. All Rights Reserved. .\" Portions Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved. .\" .TH TR 1 "June 13, 2021" .SH NAME tr \- translate characters .SH SYNOPSIS .nf \fB/usr/bin/tr\fR [\fB-cds\fR] [\fIstring1\fR [\fIstring2\fR]] .fi .LP .nf \fB/usr/xpg4/bin/tr\fR [\fB-cs\fR] \fIstring1\fR \fIstring2\fR .fi .LP .nf \fB/usr/xpg4/bin/tr\fR \fB-s\fR | \fB-d\fR [\fB-c\fR] \fIstring1\fR .fi .LP .nf \fB/usr/xpg4/bin/tr\fR \fB-ds\fR [\fB-c\fR] \fIstring1\fR \fIstring2\fR .fi .LP .nf \fB/usr/xpg6/bin/tr\fR [\fB-c\fR | \fB-C\fR] [\fB-s\fR] \fIstring1\fR \fIstring2\fR .fi .LP .nf \fB/usr/xpg6/bin/tr\fR \fB-s\fR [\fB-c\fR | \fB-C\fR] \fIstring1\fR .fi .LP .nf \fB/usr/xpg6/bin/tr\fR \fB-d\fR [\fB-c\fR | \fB-C\fR] \fIstring1\fR .fi .LP .nf \fB/usr/xpg6/bin/tr\fR \fB-ds\fR [\fB-c\fR | \fB-C\fR] \fIstring1\fR \fIstring2\fR .fi .SH DESCRIPTION The \fBtr\fR utility copies the standard input to the standard output with substitution or deletion of selected characters. The options specified and the \fIstring1\fR and \fIstring2\fR operands control translations that occur while copying characters and single-character collating elements. .SH OPTIONS The following options are supported: .sp .ne 2 .na \fB\fB-c\fR\fR .ad .RS 6n Complements the set of values specified by \fIstring1\fR. .RE .sp .ne 2 .na \fB\fB-C\fR\fR .ad .RS 6n Complements the set of characters specified by \fIstring1\fR. .RE .sp .ne 2 .na \fB\fB-d\fR\fR .ad .RS 6n Deletes all occurrences of input characters that are specified by \fIstring1\fR. .RE .sp .ne 2 .na \fB\fB-s\fR\fR .ad .RS 6n Replaces instances of repeated characters with a single character. .RE .sp .LP When the \fB-d\fR option is not specified: .RS +4 .TP .ie t \(bu .el o Each input character found in the array specified by \fIstring1\fR is replaced by the character in the same relative position in the array specified by \fIstring2\fR. When the array specified by \fIstring2\fR is shorter than the one specified by \fIstring1\fR, the results are unspecified. .RE .RS +4 .TP .ie t \(bu .el o If the \fB-c\fR option is specified, the complements of the values specified by \fIstring1\fR are placed in the array in ascending order by binary value. .RE .RS +4 .TP .ie t \(bu .el o If the \fB-C\fR option is specified, the complements of the characters specified by \fIstring1\fR (the set of all characters in the current character set, as defined by the current setting of \fBLC_CTYPE\fR, except for those actually specified in the \fIstring1\fR operand) are placed in the array in ascending collation sequence, as defined by the current setting of \fBLC_COLLATE\fR. .RE .RS +4 .TP .ie t \(bu .el o 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. .RE .sp .LP When the \fB-d\fR option is specified: .RS +4 .TP .ie t \(bu .el o Input characters found in the array specified by \fIstring1\fR are deleted. .RE .RS +4 .TP .ie t \(bu .el o When the \fB-C\fR option is specified with \fB-d\fR, all values except those specified by \fIstring1\fR are deleted. The contents of \fIstring2\fR are ignored, unless the \fB-s\fR option is also specified. .RE .RS +4 .TP .ie t \(bu .el o If the \fB-c\fR option is specified, the complements of the values specified by \fIstring1\fR are placed in the array in ascending order by binary value. .RE .RS +4 .TP .ie t \(bu .el o The same string cannot be used for both the \fB-d\fR and the \fB-s\fR option. When both options are specified, both \fIstring1\fR (used for deletion) and \fIstring2\fR (used for squeezing) are required. .RE .sp .LP When the \fB-s\fR 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: .sp .in +2 .nf \fBtr -s '[:space:]'\fR .fi .in -2 .sp .sp .LP the last operand's array contains all of the characters in that character class. However, in a case conversion, as described previously, such as .sp .in +2 .nf \fBtr -s '[:upper:]' '[:lower:]'\fR .fi .in -2 .sp .sp .LP the last operand's array contains only those characters defined as the second characters in each of the \fBtoupper\fR or \fBtolower\fR character pairs, as appropriate. (See \fBtoupper\fR(3C) and \fBtolower\fR(3C)). .sp .LP An empty string used for \fIstring1\fR or \fIstring2\fR produces undefined results. .SH OPERANDS The following operands are supported: .sp .ne 2 .na \fB\fIstring1\fR\fR .ad .br .na \fB\fIstring2\fR\fR .ad .RS 11n Translation control strings. Each string represents a set of characters to be converted into an array of characters used for the translation. .RE .sp .LP The operands \fIstring1\fR and \fIstring2\fR (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, \fBtr\fR excludes, without a diagnostic, those multi-character elements from the resulting array. .sp .ne 2 .na \fB\fIcharacter\fR\fR .ad .RS 15n Any character not described by one of the conventions below represents itself. .RE .sp .ne 2 .na \fB\fB\e\fR\fIoctal\fR\fR .ad .RS 15n Octal sequences can be used to represent characters with specific coded values. An octal sequence consists of a backslash followed by the longest sequence of one-, two-, or three-octal-digit characters (01234567). The sequence causes the character 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 \e for each byte. .RE .sp .ne 2 .na \fB\fB\e\fR\fIcharacter\fR\fR .ad .RS 15n The backslash-escape sequences \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, and \fB\ev\fR are supported. The results of using any other character, other than an octal digit, following the backslash are unspecified. .RE .SS "/usr/xpg4/bin/tr" .ne 2 .na \fB\fIc-c\fR\fR .ad .RS 7n .RE .SS "/usr/bin/tr" .ne 2 .na \fB[\fIc-c\fR]\fR .ad .RS 13n In the POSIX locale, this construct represents the range of collating elements between the range endpoints (as long as neither endpoint is an octal sequence of the form \fB\e\fR\fIoctal\fR), inclusively, as defined by the collation sequence. The characters or collating elements in the range are 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. .sp If either or both of the range endpoints are octal sequences of the form \fB\e\fR\fIoctal\fR, represents the range of specific coded binary values between two range endpoints, inclusively. .RE .sp .ne 2 .na \fB\fB[:\fR\fIclass\fR\fB:]\fR\fR .ad .RS 13n Represents all characters belonging to the defined character class, as defined by the current setting of the \fBLC_CTYPE\fR locale category. The following character class names are accepted when specified in \fIstring1\fR: .sp .in +2 .nf alnum blank digit lower punct upper alpha cntrl graph print space xdigit .fi .in -2 .sp In addition, character class expressions of the form \fB[:\fR\fIname\fR\fB:]\fR are recognized in those locales where the \fIname\fR keyword has been given a \fBcharclass\fR definition in the \fBLC_CTYPE\fR category. .sp When both the \fB-d\fR and \fB-s\fR options are specified, any of the character class names are accepted in \fIstring2\fR. Otherwise, only character class names \fBlower\fR or \fBupper\fR are valid in \fIstring2\fR and then only if the corresponding character class \fBupper\fR and \fBlower\fR, respectively, is specified in the same relative position in \fIstring1\fR. Such a specification is interpreted as a request for case conversion. When \fB[:lower:]\fR appears in \fIstring1\fR and \fB[:upper:]\fR appears in \fIstring2\fR, the arrays contain the characters from the \fBtoupper\fR mapping in the \fBLC_CTYPE\fR category of the current locale. When \fB[:upper:]\fR appears in \fIstring1\fR and \fB[:lower:]\fR appears in \fIstring2\fR, the arrays contain the characters from the \fBtolower\fR mapping in the \fBLC_CTYPE\fR category of the current locale. The first character from each mapping pair is in the array for \fIstring1\fR and the second character from each mapping pair is in the array for \fIstring2\fR in the same relative position. .sp Except for case conversion, the characters specified by a character class expression are placed in the array in an unspecified order. .sp If the name specified for \fIclass\fR does not define a valid character class in the current locale, the behavior is undefined. .RE .sp .ne 2 .na \fB\fB[=\fR\fIequiv\fR\fB=]\fR\fR .ad .RS 13n Represents all characters or collating elements belonging to the same equivalence class as \fIequiv\fR, as defined by the current setting of the \fBLC_COLLATE\fR locale category. An equivalence class expression is allowed only in \fIstring1\fR, or in \fIstring2\fR when it is being used by the combined \fB-d\fR and \fB-s\fR options. The characters belonging to the equivalence class are placed in the array in an unspecified order. .RE .sp .ne 2 .na \fB[\fIx*n\fR]\fR .ad .RS 13n Represents \fIn\fR repeated occurrences of the character \fIx\fR. Because this expression is used to map multiple characters to one, it is only valid when it occurs in \fIstring2\fR. If \fIn\fR has a leading \fB0\fR, it is interpreted as an octal value. Otherwise, it is interpreted as a decimal value. .sp If \fIn\fR is omitted or is \fB0\fR, \fB/usr/bin/tr\fR interprets this as huge; \fB/usr/xpg4/bin/tr\fR and \fB/usr/xpg6/bin/tr\fR interprets this as large enough to extend the \fIstring2\fR-based sequence to the length of the \fIstring1\fR-based sequence. .RE .SH USAGE See \fBlargefile\fR(7) for the description of the behavior of \fBtr\fR when encountering files greater than or equal to 2 Gbyte ( 2^31 bytes). .SH EXAMPLES \fBExample 1 \fRCreating a list of words .sp .LP The following example creates a list of all words in \fIfile1\fR, one per line in \fIfile2\fR, where a word is taken to be a maximal string of letters. .sp .in +2 .nf \fBtr \(mics "[:alpha:]" "[\en*]" file2\fR .fi .in -2 .sp .LP \fBExample 2 \fRTranslating characters .sp .LP This example translates all lower-case characters in \fBfile1\fR to upper-case and writes the results to standard output. .sp .in +2 .nf \fBtr "[:lower:]" "[:upper:]" file2\fR .fi .in -2 .sp .SH ENVIRONMENT VARIABLES See \fBenviron\fR(7) for descriptions of the following environment variables that affect the execution of \fBtr\fR: \fBLANG\fR, \fBLC_ALL\fR, \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR. .SH EXIT STATUS The following exit values are returned: .sp .ne 2 .na \fB\fB0\fR\fR .ad .RS 6n All input was processed successfully. .RE .sp .ne 2 .na \fB\fB>0\fR\fR .ad .RS 6n An error occurred. .RE .SH ATTRIBUTES See \fBattributes\fR(7) for descriptions of the following attributes: .SS "/usr/bin/tr" .TS box; c | c l | l . ATTRIBUTE TYPE ATTRIBUTE VALUE _ CSI Enabled .TE .SS "/usr/xpg4/bin/tr" .TS box; c | c l | l . ATTRIBUTE TYPE ATTRIBUTE VALUE _ CSI Enabled _ Interface Stability Committed _ Standard See \fBstandards\fR(7). .TE .SS "/usr/xpg6/bin/tr" .TS box; c | c l | l . ATTRIBUTE TYPE ATTRIBUTE VALUE _ CSI Enabled _ Interface Stability Committed _ Standard See \fBstandards\fR(7). .TE .SH SEE ALSO .BR ed (1), .BR sed (1), .BR sh (1), .BR tolower (3C), .BR toupper (3C), .BR ascii (7), .BR attributes (7), .BR environ (7), .BR largefile (7), .BR regex (7), .BR standards (7) .SH NOTES Unlike some previous versions, \fB/usr/xpg4/bin/tr\fR correctly processes \fBNUL\fR characters in its input stream. \fBNUL\fR characters can be stripped by using \fBtr\fR \fB-d\fR \fB\&'\e000'\fR.