xref: /freebsd/usr.bin/tr/tr.1 (revision 3baccdb9fd44df1dd289f49ec2a7a0e25064d45e)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the Institute of Electrical and Electronics Engineers, Inc.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)tr.1	8.1 (Berkeley) 6/6/93
36.\" $FreeBSD$
37.\"
38.Dd July 23, 2004
39.Dt TR 1
40.Os
41.Sh NAME
42.Nm tr
43.Nd translate characters
44.Sh SYNOPSIS
45.Nm
46.Op Fl Ccsu
47.Ar string1 string2
48.Nm
49.Op Fl Ccu
50.Fl d
51.Ar string1
52.Nm
53.Op Fl Ccu
54.Fl s
55.Ar string1
56.Nm
57.Op Fl Ccu
58.Fl ds
59.Ar string1 string2
60.Sh DESCRIPTION
61The
62.Nm
63utility copies the standard input to the standard output with substitution
64or deletion of selected characters.
65.Pp
66The following options are available:
67.Bl -tag -width Ds
68.It Fl C
69Complement the set of characters in
70.Ar string1 ,
71that is
72.Dq Fl C Li ab
73includes every character except for
74.Ql a
75and
76.Ql b .
77.It Fl c
78Same as
79.Fl C
80but complement the set of values in
81.Ar string1 .
82.It Fl d
83Delete characters in
84.Ar string1
85from the input.
86.It Fl s
87Squeeze multiple occurrences of the characters listed in the last
88operand (either
89.Ar string1
90or
91.Ar string2 )
92in the input into a single instance of the character.
93This occurs after all deletion and translation is completed.
94.It Fl u
95Guarantee that any output is unbuffered.
96.El
97.Pp
98In the first synopsis form, the characters in
99.Ar string1
100are translated into the characters in
101.Ar string2
102where the first character in
103.Ar string1
104is translated into the first character in
105.Ar string2
106and so on.
107If
108.Ar string1
109is longer than
110.Ar string2 ,
111the last character found in
112.Ar string2
113is duplicated until
114.Ar string1
115is exhausted.
116.Pp
117In the second synopsis form, the characters in
118.Ar string1
119are deleted from the input.
120.Pp
121In the third synopsis form, the characters in
122.Ar string1
123are compressed as described for the
124.Fl s
125option.
126.Pp
127In the fourth synopsis form, the characters in
128.Ar string1
129are deleted from the input, and the characters in
130.Ar string2
131are compressed as described for the
132.Fl s
133option.
134.Pp
135The following conventions can be used in
136.Ar string1
137and
138.Ar string2
139to specify sets of characters:
140.Bl -tag -width [:equiv:]
141.It character
142Any character not described by one of the following conventions
143represents itself.
144.It \eoctal
145A backslash followed by 1, 2 or 3 octal digits represents a character
146with that encoded value.
147To follow an octal sequence with a digit as a character, left zero-pad
148the octal sequence to the full 3 octal digits.
149.It \echaracter
150A backslash followed by certain special characters maps to special
151values.
152.Pp
153.Bl -column "\ea"
154.It "\ea	<alert character>
155.It "\eb	<backspace>
156.It "\ef	<form-feed>
157.It "\en	<newline>
158.It "\er	<carriage return>
159.It "\et	<tab>
160.It "\ev	<vertical tab>
161.El
162.Pp
163A backslash followed by any other character maps to that character.
164.It c-c
165For non-octal range endpoints
166represents the range of characters between the range endpoints, inclusive,
167in ascending order,
168as defined by the collation sequence.
169If either or both of the range endpoints are octal sequences, it
170represents the range of specific coded values between the
171range endpoints, inclusive.
172.Pp
173.Bf Em
174See the
175.Sx COMPATIBILITY
176section below for an important note regarding
177differences in the way the current
178implementation interprets range expressions differently from
179previous implementations.
180.Ef
181.It [:class:]
182Represents all characters belonging to the defined character class.
183Class names are:
184.Pp
185.Bl -column "xdigit"
186.It "alnum	<alphanumeric characters>
187.It "alpha	<alphabetic characters>
188.It "cntrl	<control characters>
189.It "digit	<numeric characters>
190.It "graph	<graphic characters>
191.It "lower	<lower-case alphabetic characters>
192.It "print	<printable characters>
193.It "punct	<punctuation characters>
194.It "space	<space characters>
195.It "upper	<upper-case characters>
196.It "xdigit	<hexadecimal characters>
197.El
198.Pp
199.\" All classes may be used in
200.\" .Ar string1 ,
201.\" and in
202.\" .Ar string2
203.\" when both the
204.\" .Fl d
205.\" and
206.\" .Fl s
207.\" options are specified.
208.\" Otherwise, only the classes ``upper'' and ``lower'' may be used in
209.\" .Ar string2
210.\" and then only when the corresponding class (``upper'' for ``lower''
211.\" and vice-versa) is specified in the same relative position in
212.\" .Ar string1 .
213.\" .Pp
214When
215.Dq Li [:lower:]
216appears in
217.Ar string1
218and
219.Dq Li [:upper:]
220appears in the same relative position in
221.Ar string2 ,
222it represents the characters pairs from the
223.Dv toupper
224mapping in the
225.Ev LC_CTYPE
226category of the current locale.
227When
228.Dq Li [:upper:]
229appears in
230.Ar string1
231and
232.Dq Li [:lower:]
233appears in the same relative position in
234.Ar string2 ,
235it represents the characters pairs from the
236.Dv tolower
237mapping in the
238.Ev LC_CTYPE
239category of the current locale.
240.Pp
241With the exception of case conversion,
242characters in the classes are in unspecified order.
243.Pp
244For specific information as to which
245.Tn ASCII
246characters are included
247in these classes, see
248.Xr ctype 3
249and related manual pages.
250.It [=equiv=]
251Represents all characters belonging to the same equivalence class as
252.Ar equiv ,
253ordered by their encoded values.
254.It [#*n]
255Represents
256.Ar n
257repeated occurrences of the character represented by
258.Ar # .
259This
260expression is only valid when it occurs in
261.Ar string2 .
262If
263.Ar n
264is omitted or is zero, it is be interpreted as large enough to extend
265.Ar string2
266sequence to the length of
267.Ar string1 .
268If
269.Ar n
270has a leading zero, it is interpreted as an octal value, otherwise,
271it's interpreted as a decimal value.
272.El
273.Sh ENVIRONMENT
274The
275.Ev LANG , LC_ALL , LC_CTYPE
276and
277.Ev LC_COLLATE
278environment variables affect the execution of
279.Nm
280as described in
281.Xr environ 7 .
282.Sh EXAMPLES
283The following examples are shown as given to the shell:
284.Pp
285Create a list of the words in file1, one per line, where a word is taken to
286be a maximal string of letters.
287.Pp
288.D1 Li "tr -cs \*q[:alpha:]\*q \*q\en\*q < file1"
289.Pp
290Translate the contents of file1 to upper-case.
291.Pp
292.D1 Li "tr \*q[:lower:]\*q \*q[:upper:]\*q < file1"
293.Pp
294(This should be preferred over the traditional
295.Ux
296idiom of
297.Dq Li "tr a-z A-Z" ,
298since it works correctly in all locales.)
299.Pp
300Strip out non-printable characters from file1.
301.Pp
302.D1 Li "tr -cd \*q[:print:]\*q < file1"
303.Pp
304Remove diacritical marks from all accented variants of the letter
305.Ql e :
306.Pp
307.Dl "tr \*q[=e=]\*q \*qe\*q"
308.Sh DIAGNOSTICS
309.Ex -std
310.Sh COMPATIBILITY
311Previous
312.Fx
313implementations of
314.Nm
315did not order characters in range expressions according to the current
316locale's collation order, making it possible to convert unaccented Latin
317characters (esp.\& as found in English text) from upper to lower case using
318the traditional
319.Ux
320idiom of
321.Dq Li "tr A-Z a-z" .
322Since
323.Nm
324now obeys the locale's collation order, this idiom may not produce
325correct results when there is not a 1:1 mapping between lower and
326upper case, or when the order of characters within the two cases differs.
327As noted in the
328.Sx EXAMPLES
329section above, the character class expressions
330.Dq Li [:lower:]
331and
332.Dq Li [:upper:]
333should be used instead of explicit character ranges like
334.Dq Li a-z
335and
336.Dq Li A-Z .
337.Pp
338System V has historically implemented character ranges using the syntax
339.Dq Li [c-c]
340instead of the
341.Dq Li c-c
342used by historic
343.Bx
344implementations and
345standardized by POSIX.
346System V shell scripts should work under this implementation as long as
347the range is intended to map in another range, i.e., the command
348.Dq Li "tr [a-z] [A-Z]"
349will work as it will map the
350.Ql \&[
351character in
352.Ar string1
353to the
354.Ql \&[
355character in
356.Ar string2 .
357However, if the shell script is deleting or squeezing characters as in
358the command
359.Dq Li "tr -d [a-z]" ,
360the characters
361.Ql \&[
362and
363.Ql \&]
364will be
365included in the deletion or compression list which would not have happened
366under a historic System V implementation.
367Additionally, any scripts that depended on the sequence
368.Dq Li a-z
369to
370represent the three characters
371.Ql a ,
372.Ql \-
373and
374.Ql z
375will have to be
376rewritten as
377.Dq Li a\e-z .
378.Pp
379The
380.Nm
381utility has historically not permitted the manipulation of NUL bytes in
382its input and, additionally, stripped NUL's from its input stream.
383This implementation has removed this behavior as a bug.
384.Pp
385The
386.Nm
387utility has historically been extremely forgiving of syntax errors,
388for example, the
389.Fl c
390and
391.Fl s
392options were ignored unless two strings were specified.
393This implementation will not permit illegal syntax.
394.Sh STANDARDS
395The
396.Nm
397utility conforms to
398.St -p1003.1-2001 .
399.Pp
400It should be noted that the feature wherein the last character of
401.Ar string2
402is duplicated if
403.Ar string2
404has less characters than
405.Ar string1
406is permitted by POSIX but is not required.
407Shell scripts attempting to be portable to other POSIX systems should use
408the
409.Dq Li [#*]
410convention instead of relying on this behavior.
411The
412.Fl u
413option is an extension to the
414.St -p1003.1-2001
415standard.
416