1tr(1) User Commands tr(1)
2
3
4
6 tr - translate characters
7
9 /usr/bin/tr [-cds] [string1 [string2]]
10
11
12 /usr/xpg4/bin/tr [-cs] string1 string2
13
14
15 /usr/xpg4/bin/tr -s | -d [-c] string1
16
17
18 /usr/xpg4/bin/tr -ds [-c] string1 string2
19
20
21 /usr/xpg6/bin/tr [-c | -C] [-s] string1 string2
22
23
24 /usr/xpg6/bin/tr -s [-c | -C] string1
25
26
27 /usr/xpg6/bin/tr -d [-c | -C] string1
28
29
30 /usr/xpg6/bin/tr -ds [-c | -C] string1 string2
31
32
34 The tr utility copies the standard input to the standard output with
35 substitution or deletion of selected characters. The options specified
36 and the string1 and string2 operands control translations that occur
37 while copying characters and single-character collating elements.
38
40 The following options are supported:
41
42 -c Complements the set of values specified by string1.
43
44
45 -C Complements the set of characters specified by string1.
46
47
48 -d Deletes all occurrences of input characters that are specified by
49 string1.
50
51
52 -s Replaces instances of repeated characters with a single charac‐
53 ter.
54
55
56
57 When the -d option is not specified:
58
59 o Each input character found in the array specified by string1
60 is replaced by the character in the same relative position
61 in the array specified by string2. When the array specified
62 by string2 is shorter than the one specified by string1, the
63 results are unspecified.
64
65 o If the -c option is specified, the complements of the values
66 specified by string1 are placed in the array in ascending
67 order by binary value.
68
69 o If the -C option is specified, the complements of the char‐
70 acters specified by string1 (the set of all characters in
71 the current character set, as defined by the current setting
72 of LC_CTYPE, except for those actually specified in the
73 string1 operand) are placed in the array in ascending colla‐
74 tion sequence, as defined by the current setting of LC_COL‐
75 LATE.
76
77 o Because the order in which characters specified by character
78 class expressions or equivalence class expressions is unde‐
79 fined, such expressions should only be used if the intent is
80 to map several characters into one. An exception is case
81 conversion, as described previously.
82
83
84 When the -d option is specified:
85
86 o Input characters found in the array specified by string1 are
87 deleted.
88
89 o When the -C option is specified with -d, all values except
90 those specified by string1 are deleted. The contents of
91 string2 are ignored, unless the -s option is also specified.
92
93 o If the -c option is specified, the complements of the values
94 specified by string1 are placed in the array in ascending
95 order by binary value.
96
97 o The same string cannot be used for both the -d and the -s
98 option. When both options are specified, both string1 (used
99 for deletion) and string2 (used for squeezing) are required.
100
101
102 When the -s option is specified, after any deletions or translations
103 have taken place, repeated sequences of the same character is replaced
104 by one occurrence of the same character, if the character is found in
105 the array specified by the last operand. If the last operand contains a
106 character class, such as the following example:
107
108 tr -s '[:space:]'
109
110
111
112
113 the last operand's array contains all of the characters in that charac‐
114 ter class. However, in a case conversion, as described previously, such
115 as
116
117 tr -s '[:upper:]' '[:lower:]'
118
119
120
121
122 the last operand's array contains only those characters defined as the
123 second characters in each of the toupper or tolower character pairs, as
124 appropriate. (See toupper(3C) and tolower(3C)).
125
126
127 An empty string used for string1 or string2 produces undefined results.
128
130 The following operands are supported:
131
132 string1 Translation control strings. Each string represents a set of
133 string2 characters to be converted into an array of characters used
134 for the translation.
135
136
137
138 The operands string1 and string2 (if specified) define two arrays of
139 characters. The constructs in the following list can be used to specify
140 characters or single-character collating elements. If any of the con‐
141 structs result in multi-character collating elements, tr excludes,
142 without a diagnostic, those multi-character elements from the resulting
143 array.
144
145 character Any character not described by one of the conventions
146 below represents itself.
147
148
149 \octal Octal sequences can be used to represent characters with
150 specific coded values. An octal sequence consists of a
151 backslash followed by the longest sequence of one-,
152 two-, or three-octal-digit characters (01234567). The
153 sequence causes the character whose encoding is repre‐
154 sented by the one-, two- or three-digit octal integer to
155 be placed into the array. Multi-byte characters require
156 multiple, concatenated escape sequences of this type,
157 including the leading \ for each byte.
158
159
160 \character The backslash-escape sequences \a, \b, \f, \n, \r, \t,
161 and \v are supported. The results of using any other
162 character, other than an octal digit, following the
163 backslash are unspecified.
164
165
166 /usr/xpg4/bin/tr
167 c-c
168
169
170 /usr/bin/tr
171 [c-c] In the POSIX locale, this construct represents the range
172 of collating elements between the range endpoints (as long
173 as neither endpoint is an octal sequence of the form
174 \octal), inclusively, as defined by the collation
175 sequence. The characters or collating elements in the
176 range are placed in the array in ascending collation
177 sequence. If the second endpoint precedes the starting
178 endpoint in the collation sequence, it is unspecified
179 whether the range of collating elements is empty, or this
180 construct is treated as invalid. In locales other than the
181 POSIX locale, this construct has unspecified behavior.
182
183 If either or both of the range endpoints are octal
184 sequences of the form \octal, represents the range of spe‐
185 cific coded binary values between two range endpoints,
186 inclusively.
187
188
189 [:class:] Represents all characters belonging to the defined charac‐
190 ter class, as defined by the current setting of the
191 LC_CTYPE locale category. The following character class
192 names are accepted when specified in string1:
193
194 alnum blank digit lower punct upper
195 alpha cntrl graph print space xdigit
196
197
198 In addition, character class expressions of the form
199 [:name:] are recognized in those locales where the name
200 keyword has been given a charclass definition in the
201 LC_CTYPE category.
202
203 When both the -d and -s options are specified, any of the
204 character class names are accepted in string2. Otherwise,
205 only character class names lower or upper are valid in
206 string2 and then only if the corresponding character class
207 upper and lower, respectively, is specified in the same
208 relative position in string1. Such a specification is
209 interpreted as a request for case conversion. When
210 [:lower:] appears in string1 and [:upper:] appears in
211 string2, the arrays contain the characters from the toup‐
212 per mapping in the LC_CTYPE category of the current
213 locale. When [:upper:] appears in string1 and [:lower:]
214 appears in string2, the arrays contain the characters from
215 the tolower mapping in the LC_CTYPE category of the cur‐
216 rent locale. The first character from each mapping pair is
217 in the array for string1 and the second character from
218 each mapping pair is in the array for string2 in the same
219 relative position.
220
221 Except for case conversion, the characters specified by a
222 character class expression are placed in the array in an
223 unspecified order.
224
225 If the name specified for class does not define a valid
226 character class in the current locale, the behavior is
227 undefined.
228
229
230 [=equiv=] Represents all characters or collating elements belonging
231 to the same equivalence class as equiv, as defined by the
232 current setting of the LC_COLLATE locale category. An
233 equivalence class expression is allowed only in string1,
234 or in string2 when it is being used by the combined -d and
235 -s options. The characters belonging to the equivalence
236 class are placed in the array in an unspecified order.
237
238
239 [x*n] Represents n repeated occurrences of the character x.
240 Because this expression is used to map multiple characters
241 to one, it is only valid when it occurs in string2. If n
242 has a leading 0, it is interpreted as an octal value.
243 Otherwise, it is interpreted as a decimal value.
244
245 If n is omitted or is 0, /usr/bin/tr interprets this as
246 huge; /usr/xpg4/bin/tr and /usr/xpg6/bin/tr interprets
247 this as large enough to extend thestring2-based sequence
248 to the length of the string1-based sequence.
249
250
252 See largefile(5) for the description of the behavior of tr when encoun‐
253 tering files greater than or equal to 2 Gbyte ( 2^31 bytes).
254
256 Example 1 Creating a list of words
257
258
259 The following example creates a list of all words in file1, one per
260 line in file2, where a word is taken to be a maximal string of letters.
261
262
263 tr −cs "[:alpha:]" "[\n*]" <file1 >file2
264
265
266
267 Example 2 Translating characters
268
269
270 This example translates all lower-case characters in file1 to upper-
271 case and writes the results to standard output.
272
273
274 tr "[:lower:]" "[:upper:]" <file1
275
276
277
278
279 Notice that the caveat expressed in the corresponding example in XPG3
280 is no longer in effect. This case conversion is now a special case that
281 employs the tolower and toupper classifications, ensuring that proper
282 mapping is accomplished (when the locale is correctly defined).
283
284
285 Example 3 Identifying equivalent characters
286
287
288 This example uses an equivalence class to identify accented variants of
289 the base character e in file1, which are stripped of diacritical marks
290 and written to file2.
291
292
293 tr "[=e=]" e <file1 >file2
294
295
296
298 See environ(5) for descriptions of the following environment variables
299 that affect the execution of tr: LANG, LC_ALL, LC_COLLATE, LC_CTYPE,
300 LC_MESSAGES, and NLSPATH.
301
303 The following exit values are returned:
304
305 0 All input was processed successfully.
306
307
308 >0 An error occurred.
309
310
312 See attributes(5) for descriptions of the following attributes:
313
314 /usr/bin/tr
315 ┌─────────────────────────────┬─────────────────────────────┐
316 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
317 ├─────────────────────────────┼─────────────────────────────┤
318 │Availability │SUNWcsu │
319 ├─────────────────────────────┼─────────────────────────────┤
320 │CSI │Enabled │
321 └─────────────────────────────┴─────────────────────────────┘
322
323 /usr/xpg4/bin/tr
324 ┌─────────────────────────────┬─────────────────────────────┐
325 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
326 ├─────────────────────────────┼─────────────────────────────┤
327 │Availability │SUNWxcu4 │
328 ├─────────────────────────────┼─────────────────────────────┤
329 │CSI │Enabled │
330 ├─────────────────────────────┼─────────────────────────────┤
331 │Interface Stability │Committed │
332 ├─────────────────────────────┼─────────────────────────────┤
333 │Standard │See standards(5). │
334 └─────────────────────────────┴─────────────────────────────┘
335
336 /usr/xpg6/bin/tr
337 ┌─────────────────────────────┬─────────────────────────────┐
338 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
339 ├─────────────────────────────┼─────────────────────────────┤
340 │Availability │SUNWxcu6 │
341 ├─────────────────────────────┼─────────────────────────────┤
342 │CSI │Enabled │
343 ├─────────────────────────────┼─────────────────────────────┤
344 │Interface Stability │Committed │
345 ├─────────────────────────────┼─────────────────────────────┤
346 │Standard │See standards(5). │
347 └─────────────────────────────┴─────────────────────────────┘
348
350 ed(1), sed(1), sh(1), tolower(3C), toupper(3C), ascii(5),
351 attributes(5), environ(5), largefile(5), regex(5), standards(5)
352
354 Unlike some previous versions, /usr/xpg4/bin/tr correctly processes NUL
355 characters in its input stream. NUL characters can be stripped by using
356 tr -d '\000'.
357
358
359
360SunOS 5.11 3 Mar 2009 tr(1)