Merge commit 'b31320a79e2054c6739b5229259dbf98f3afc547' into merges
[unleashed.git] / share / man / man1 / tr.1
blob6221faaef7815e76d9192764a1473795b2e57d2d
1 '\" te
2 .\" Copyright 1989 AT&T Copyright (c) 1992, X/Open Company Limited All Rights Reserved
3 .\" Portions Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved
4 .\" 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/.
5 .\" 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
6 .\" are reprinted and reproduced in electronic form in the Sun OS 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
7 .\" 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.
8 .\"  This notice shall appear on any product containing this material.
9 .\" 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.
10 .\"  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
11 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH TR 1 "Oct 25, 2017"
13 .SH NAME
14 tr \- translate characters
15 .SH SYNOPSIS
16 .LP
17 .nf
18 \fB/usr/bin/tr\fR [\fB-c\fR | \fB-C\fR] [\fB-s\fR] \fIstring1\fR \fIstring2\fR
19 .fi
21 .LP
22 .nf
23 \fB/usr/bin/tr\fR \fB-s\fR [\fB-c\fR | \fB-C\fR] \fIstring1\fR
24 .fi
26 .LP
27 .nf
28 \fB/usr/bin/tr\fR \fB-d\fR [\fB-c\fR | \fB-C\fR] \fIstring1\fR
29 .fi
31 .LP
32 .nf
33 \fB/usr/bin/tr\fR \fB-ds\fR [\fB-c\fR | \fB-C\fR] \fIstring1\fR \fIstring2\fR
34 .fi
36 .SH DESCRIPTION
37 .sp
38 .LP
39 The \fBtr\fR utility copies the standard input to the standard output with
40 substitution or deletion of selected characters. The options specified and the
41 \fIstring1\fR and \fIstring2\fR operands control translations that occur while
42 copying characters and single-character collating elements.
43 .SH OPTIONS
44 .sp
45 .LP
46 The following options are supported:
47 .sp
48 .ne 2
49 .na
50 \fB\fB-c\fR\fR
51 .ad
52 .RS 6n
53 Complements the set of values specified by \fIstring1\fR.
54 .RE
56 .sp
57 .ne 2
58 .na
59 \fB\fB-C\fR\fR
60 .ad
61 .RS 6n
62 Complements the set of characters specified by \fIstring1\fR.
63 .RE
65 .sp
66 .ne 2
67 .na
68 \fB\fB-d\fR\fR
69 .ad
70 .RS 6n
71 Deletes all occurrences of input characters that are specified by
72 \fIstring1\fR.
73 .RE
75 .sp
76 .ne 2
77 .na
78 \fB\fB-s\fR\fR
79 .ad
80 .RS 6n
81 Replaces instances of repeated characters with a single character.
82 .RE
84 .sp
85 .LP
86 When the \fB-d\fR option is not specified:
87 .RS +4
88 .TP
89 .ie t \(bu
90 .el o
91 Each input character found in the array specified by \fIstring1\fR is replaced
92 by the character in the same relative position in the array specified by
93 \fIstring2\fR. When the array specified by \fIstring2\fR is shorter than the
94 one specified by \fIstring1\fR, the results are unspecified.
95 .RE
96 .RS +4
97 .TP
98 .ie t \(bu
99 .el o
100 If the \fB-c\fR option is specified, the complements of the values specified by
101 \fIstring1\fR are placed in the array in ascending order by binary value.
103 .RS +4
105 .ie t \(bu
106 .el o
107 If the \fB-C\fR option is specified, the complements of the characters
108 specified by \fIstring1\fR (the set of all characters in the current character
109 set, as defined by the current setting of \fBLC_CTYPE\fR, except for those
110 actually specified in the \fIstring1\fR operand) are placed in the array in
111 ascending collation sequence, as defined by the current setting of
112 \fBLC_COLLATE\fR.
114 .RS +4
116 .ie t \(bu
117 .el o
118 Because the order in which characters specified by character class expressions
119 or equivalence class expressions is undefined, such expressions should only be
120 used if the intent is to map several characters into one. An exception is case
121 conversion, as described previously.
125 When the \fB-d\fR option is specified:
126 .RS +4
128 .ie t \(bu
129 .el o
130 Input characters found in the array specified by \fIstring1\fR are deleted.
132 .RS +4
134 .ie t \(bu
135 .el o
136 When the \fB-C\fR option is specified with \fB-d\fR, all values except those
137 specified by \fIstring1\fR are deleted. The contents of \fIstring2\fR are
138 ignored, unless the \fB-s\fR option is also specified.
140 .RS +4
142 .ie t \(bu
143 .el o
144 If the \fB-c\fR option is specified, the complements of the values specified by
145 \fIstring1\fR are placed in the array in ascending order by binary value.
147 .RS +4
149 .ie t \(bu
150 .el o
151 The same string cannot be used for both the \fB-d\fR and the \fB-s\fR option.
152 When both options are specified, both \fIstring1\fR (used for deletion) and
153 \fIstring2\fR (used for squeezing) are required.
157 When the \fB-s\fR option is specified, after any deletions or translations have
158 taken place, repeated sequences of the same character is replaced by one
159 occurrence of the same character, if the character is found in the array
160 specified by the last operand. If the last operand contains a character class,
161 such as the following example:
163 .in +2
165 \fBtr -s '[:space:]'\fR
167 .in -2
172 the last operand's array contains all of the characters in that character
173 class. However, in a case conversion, as described previously, such as
175 .in +2
177 \fBtr -s '[:upper:]' '[:lower:]'\fR
179 .in -2
184 the last operand's array contains only those characters defined as the second
185 characters in each of the \fBtoupper\fR or \fBtolower\fR character pairs, as
186 appropriate. (See \fBtoupper\fR(3C) and \fBtolower\fR(3C)).
189 An empty string used for \fIstring1\fR or \fIstring2\fR produces undefined
190 results.
191 .SH OPERANDS
194 The following operands are supported:
196 .ne 2
198 \fB\fIstring1\fR\fR
202 \fB\fIstring2\fR\fR
204 .RS 11n
205 Translation control strings. Each string represents a set of characters to be
206 converted into an array of characters used for the translation.
211 The operands \fIstring1\fR and \fIstring2\fR (if specified) define two arrays
212 of characters. The constructs in the following list can be used to specify
213 characters or single-character collating elements. If any of the constructs
214 result in multi-character collating elements, \fBtr\fR excludes, without a
215 diagnostic, those multi-character elements from the resulting array.
217 .ne 2
219 \fB\fIcharacter\fR\fR
221 .RS 15n
222 Any character not described by one of the conventions below represents itself.
226 .ne 2
228 \fB\fB\e\fR\fIoctal\fR\fR
230 .RS 15n
231 Octal sequences can be used to represent characters with specific coded values.
232 An octal sequence consists of a backslash followed by the longest sequence of
233 one-, two-, or three-octal-digit characters (01234567). The sequence causes the
234 character whose encoding is represented by the one-, two- or three-digit octal
235 integer to be placed into the array. Multi-byte characters require multiple,
236 concatenated escape sequences of this type, including the leading \e for each
237 byte.
241 .ne 2
243 \fB\fB\e\fR\fIcharacter\fR\fR
245 .RS 15n
246 The backslash-escape sequences \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR,
247 \fB\er\fR, \fB\et\fR, and \fB\ev\fR are supported. The results of using any
248 other character, other than an octal digit, following the backslash are
249 unspecified.
253 .ne 2
255 \fB[\fIc-c\fR]\fR
257 .RS 13n
258 In the POSIX locale, this construct represents the range of collating elements
259 between the range endpoints (as long as neither endpoint is an octal sequence
260 of the form \fB\e\fR\fIoctal\fR), inclusively, as defined by the collation
261 sequence. The characters or collating elements in the range are placed in the
262 array in ascending collation sequence. If the second endpoint precedes the
263 starting endpoint in the collation sequence, it is unspecified whether the
264 range of collating elements is empty, or this construct is treated as invalid.
265 In locales other than the POSIX locale, this construct has unspecified
266 behavior.
268 If either or both of the range endpoints are octal sequences of the form
269 \fB\e\fR\fIoctal\fR, represents the range of specific coded binary values
270 between two range endpoints, inclusively.
274 .ne 2
276 \fB\fB[:\fR\fIclass\fR\fB:]\fR\fR
278 .RS 13n
279 Represents all characters belonging to the defined character class, as defined
280 by the current setting of the \fBLC_CTYPE\fR locale category. The following
281 character class names are accepted when specified in \fIstring1\fR:
283 .in +2
285 alnum  blank  digit  lower  punct  upper
286 alpha  cntrl  graph  print  space  xdigit
288 .in -2
291 In addition, character class expressions of the form \fB[:\fR\fIname\fR\fB:]\fR
292 are recognized in those locales where the \fIname\fR keyword has been given a
293 \fBcharclass\fR definition in the \fBLC_CTYPE\fR category.
295 When both the \fB-d\fR and \fB-s\fR options are specified, any of the character
296 class names are accepted in \fIstring2\fR. Otherwise, only character class
297 names \fBlower\fR or \fBupper\fR are valid in \fIstring2\fR and then only if
298 the corresponding character class \fBupper\fR and \fBlower\fR, respectively, is
299 specified in the same relative position in \fIstring1\fR. Such a specification
300 is interpreted as a request for case conversion. When \fB[:lower:]\fR appears
301 in \fIstring1\fR and \fB[:upper:]\fR appears in \fIstring2,\fR the arrays
302 contain the characters from the \fBtoupper\fR mapping in the \fBLC_CTYPE\fR
303 category of the current locale. When \fB[:upper:]\fR appears in \fIstring1\fR
304 and \fB[:lower:]\fR appears in \fIstring2,\fR the arrays contain the characters
305 from the \fBtolower\fR mapping in the \fBLC_CTYPE\fR category of the current
306 locale. The first character from each mapping pair is in the array for
307 \fIstring1\fR and the second character from each mapping pair is in the array
308 for \fIstring2\fR in the same relative position.
310 Except for case conversion, the characters specified by a character class
311 expression are placed in the array in an unspecified order.
313 If the name specified for \fIclass\fR does not define a valid character class
314 in the current locale, the behavior is undefined.
318 .ne 2
320 \fB\fB[=\fR\fIequiv\fR\fB=]\fR\fR
322 .RS 13n
323 Represents all characters or collating elements belonging to the same
324 equivalence class as \fIequiv\fR, as defined by the current setting of the
325 \fBLC_COLLATE\fR locale category. An equivalence class expression is allowed
326 only in \fIstring1\fR, or in \fIstring2\fR when it is being used by the
327 combined \fB-d\fR and \fB-s\fR options. The characters belonging to the
328 equivalence class are placed in the array in an unspecified order.
332 .ne 2
334 \fB[\fIx*n\fR]\fR
336 .RS 13n
337 Represents \fIn\fR repeated occurrences of the character \fIx\fR. Because this
338 expression is used to map multiple characters to one, it is only valid when it
339 occurs in \fIstring2\fR. If \fIn\fR has a leading \fB0\fR, it is interpreted
340 as an octal value. Otherwise, it is interpreted as a decimal value.
342 If \fIn\fR is omitted or is \fB0\fR, \fB/usr/bin/tr\fR interprets this as large
343 enough to extend the\fIstring2\fR-based sequence to the length of the
344 \fIstring1\fR-based sequence.
347 .SH USAGE
350 See \fBlargefile\fR(5) for the description of the behavior of \fBtr\fR when
351 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
352 .SH EXAMPLES
354 \fBExample 1 \fRCreating a list of words
357 The following example creates a list of all words in \fIfile1\fR, one per line
358 in \fIfile2\fR, where a word is taken to be a maximal string of letters.
361 .in +2
363 \fBtr \(mics "[:alpha:]" "[\en*]" <file1 >file2\fR
365 .in -2
369 \fBExample 2 \fRTranslating characters
372 This example translates all lower-case characters in \fBfile1\fR to upper-case
373 and writes the results to standard output.
376 .in +2
378 \fBtr "[:lower:]" "[:upper:]" <file1\fR
380 .in -2
385 Notice that the caveat expressed in the corresponding example in XPG3 is no
386 longer in effect. This case conversion is now a special case that employs the
387 \fBtolower\fR and \fBtoupper\fR classifications, ensuring that proper mapping
388 is accomplished (when the locale is correctly defined).
391 \fBExample 3 \fRIdentifying equivalent characters
394 This example uses an equivalence class to identify accented variants of the
395 base character \fBe\fR in \fBfile1\fR, which are stripped of diacritical marks
396 and written to \fBfile2\fR.
399 .in +2
401 \fBtr "[=e=]" e <file1 >file2\fR
403 .in -2
406 .SH ENVIRONMENT VARIABLES
409 See \fBenviron\fR(5) for descriptions of the following environment variables
410 that affect the execution of \fBtr\fR: \fBLANG\fR, \fBLC_ALL\fR,
411 \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
412 .SH EXIT STATUS
415 The following exit values are returned:
417 .ne 2
419 \fB\fB0\fR\fR
421 .RS 6n
422 All input was processed successfully.
426 .ne 2
428 \fB\fB>0\fR\fR
430 .RS 6n
431 An error occurred.
434 .SH ATTRIBUTES
437 See \fBattributes\fR(5) for descriptions of the following attributes:
438 .SS "/usr/bin/tr"
443 box;
444 c | c
445 l | l .
446 ATTRIBUTE TYPE  ATTRIBUTE VALUE
448 CSI     Enabled
450 Interface Stability     Committed
452 Standard        See \fBstandards\fR(5).
455 .SH SEE ALSO
458 \fBed\fR(1), \fBsed\fR(1), \fBsh\fR(1), \fBtolower\fR(3C), \fBtoupper\fR(3C),
459 \fBascii\fR(5), \fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5),
460 \fBregex\fR(5), \fBstandards\fR(5)
461 .SH NOTES
464 Unlike some previous versions, \fB/usr/bin/tr\fR correctly processes
465 \fBNUL\fR characters in its input stream. \fBNUL\fR characters can be stripped
466 by using \fBtr\fR \fB-d\fR \fB\&'\e000'\fR.