xref: /dragonfly/usr.bin/tr/tr.1 (revision 9ddb8543)
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: src/usr.bin/tr/tr.1,v 1.5.2.7 2002/07/29 12:59:33 tjr Exp $
37.\" $DragonFly: src/usr.bin/tr/tr.1,v 1.4 2008/05/02 02:05:07 swildner Exp $
38.\"
39.Dd October 11, 1997
40.Dt TR 1
41.Os
42.Sh NAME
43.Nm tr
44.Nd translate characters
45.Sh SYNOPSIS
46.Nm
47.Op Fl csu
48.Ar string1 string2
49.Nm
50.Op Fl cu
51.Fl d
52.Ar string1
53.Nm
54.Op Fl cu
55.Fl s
56.Ar string1
57.Nm
58.Op Fl cu
59.Fl ds
60.Ar string1 string2
61.Sh DESCRIPTION
62The
63.Nm
64utility copies the standard input to the standard output with substitution
65or deletion of selected characters.
66.Pp
67The following options are available:
68.Bl -tag -width Ds
69.It Fl c
70Complements the set of characters in
71.Ar string1 ,
72that is ``-c ab'' includes every character except for ``a'' and ``b''.
73.It Fl d
74The
75.Fl d
76option causes characters to be deleted from the input.
77.It Fl s
78The
79.Fl s
80option squeezes multiple occurrences of the characters listed in the last
81operand (either
82.Ar string1
83or
84.Ar string2 )
85in the input into a single instance of the character.
86This occurs after all deletion and translation is completed.
87.It Fl u
88The
89.Fl u
90option guarantees that any output is unbuffered.
91.El
92.Pp
93In the first synopsis form, the characters in
94.Ar string1
95are translated into the characters in
96.Ar string2
97where the first character in
98.Ar string1
99is translated into the first character in
100.Ar string2
101and so on.
102If
103.Ar string1
104is longer than
105.Ar string2 ,
106the last character found in
107.Ar string2
108is duplicated until
109.Ar string1
110is exhausted.
111.Pp
112In the second synopsis form, the characters in
113.Ar string1
114are deleted from the input.
115.Pp
116In the third synopsis form, the characters in
117.Ar string1
118are compressed as described for the
119.Fl s
120option.
121.Pp
122In the fourth synopsis form, the characters in
123.Ar string1
124are deleted from the input, and the characters in
125.Ar string2
126are compressed as described for the
127.Fl s
128option.
129.Pp
130The following conventions can be used in
131.Ar string1
132and
133.Ar string2
134to specify sets of characters:
135.Bl -tag -width [:equiv:]
136.It character
137Any character not described by one of the following conventions
138represents itself.
139.It \eoctal
140A backslash followed by 1, 2 or 3 octal digits represents a character
141with that encoded value.
142To follow an octal sequence with a digit as a character, left zero-pad
143the octal sequence to the full 3 octal digits.
144.It \echaracter
145A backslash followed by certain special characters maps to special
146values.
147.Bl -column "\ea"
148.It "\ea	<alert character>"
149.It "\eb	<backspace>"
150.It "\ef	<form-feed>"
151.It "\en	<newline>"
152.It "\er	<carriage return>"
153.It "\et	<tab>"
154.It "\ev	<vertical tab>"
155.El
156.Pp
157A backslash followed by any other character maps to that character.
158.It c-c
159Represents the range of characters between the range endpoints, inclusively.
160.It [:class:]
161Represents all characters belonging to the defined character class.
162Class names are:
163.Bl -column "xdigit"
164.It "alnum	<alphanumeric characters>
165.It "alpha	<alphabetic characters>
166.It "cntrl	<control characters>
167.It "digit	<numeric characters>
168.It "graph	<graphic characters>
169.It "lower	<lower-case alphabetic characters>
170.It "print	<printable characters>
171.It "punct	<punctuation characters>
172.It "space	<space characters>
173.It "upper	<upper-case characters>
174.It "xdigit	<hexadecimal characters>
175.El
176.Pp
177.\" All classes may be used in
178.\" .Ar string1 ,
179.\" and in
180.\" .Ar string2
181.\" when both the
182.\" .Fl d
183.\" and
184.\" .Fl s
185.\" options are specified.
186.\" Otherwise, only the classes ``upper'' and ``lower'' may be used in
187.\" .Ar string2
188.\" and then only when the corresponding class (``upper'' for ``lower''
189.\" and vice-versa) is specified in the same relative position in
190.\" .Ar string1 .
191.\" .Pp
192With the exception of the ``upper'' and ``lower'' classes, characters
193in the classes are in unspecified order.
194In the ``upper'' and ``lower'' classes, characters are entered in
195ascending order.
196.Pp
197For specific information as to which ASCII characters are included
198in these classes, see
199.Xr ctype 3
200and related manual pages.
201.It [=equiv=]
202Represents all characters belonging to the same equivalence class as
203.Ar equiv ,
204ordered by their encoded values.
205.It [#*n]
206Represents
207.Ar n
208repeated occurrences of the character represented by
209.Ar # .
210This
211expression is only valid when it occurs in
212.Ar string2 .
213If
214.Ar n
215is omitted or is zero, it is be interpreted as large enough to extend
216.Ar string2
217sequence to the length of
218.Ar string1 .
219If
220.Ar n
221has a leading zero, it is interpreted as an octal value, otherwise,
222it's interpreted as a decimal value.
223.El
224.Sh ENVIRONMENT
225The
226.Ev LANG ,
227.Ev LC_ALL ,
228.Ev LC_CTYPE
229and
230.Ev LC_COLLATE
231environment variables affect the execution of
232.Nm
233as described in
234.Xr environ 7 .
235.Sh EXIT STATUS
236.Ex -std
237.Sh EXAMPLES
238The following examples are shown as given to the shell:
239.Pp
240Create a list of the words in file1, one per line, where a word is taken to
241be a maximal string of letters.
242.Pp
243.D1 Li "tr -cs \*q[:alpha:]\*q \*q\en\*q < file1"
244.Pp
245Translate the contents of file1 to upper-case.
246.Pp
247.D1 Li "tr \*q[:lower:]\*q \*q[:upper:]\*q < file1"
248.Pp
249Strip out non-printable characters from file1.
250.Pp
251.D1 Li "tr -cd \*q[:print:]\*q < file1"
252.Pp
253Remove diacritical marks from all accented variants of the letter
254.Sq e :
255.Pp
256.Dl "tr \*q[=e=]\*q \*qe\*q"
257.Sh COMPATIBILITY
258System V has historically implemented character ranges using the syntax
259``[c-c]'' instead of the ``c-c'' used by historic
260.Bx
261implementations and
262standardized by POSIX.
263System V shell scripts should work under this implementation as long as
264the range is intended to map in another range, i.e. the command
265``tr [a-z] [A-Z]'' will work as it will map the ``['' character in
266.Ar string1
267to the ``['' character in
268.Ar string2 .
269However, if the shell script is deleting or squeezing characters as in
270the command ``tr -d [a-z]'', the characters ``['' and ``]'' will be
271included in the deletion or compression list which would not have happened
272under an historic System V implementation.
273Additionally, any scripts that depended on the sequence ``a-z'' to
274represent the three characters ``a'', ``-'' and ``z'' will have to be
275rewritten as ``a\e-z''.
276.Pp
277The
278.Nm
279utility has historically not permitted the manipulation of NUL bytes in
280its input and, additionally, stripped NUL's from its input stream.
281This implementation has removed this behavior as a bug.
282.Pp
283The
284.Nm
285utility has historically been extremely forgiving of syntax errors,
286for example, the
287.Fl c
288and
289.Fl s
290options were ignored unless two strings were specified.
291This implementation will not permit illegal syntax.
292.Sh STANDARDS
293The
294.Nm
295utility is expected to be
296.St -p1003.2
297compatible.
298It should be noted that the feature wherein the last character of
299.Ar string2
300is duplicated if
301.Ar string2
302has less characters than
303.Ar string1
304is permitted by POSIX but is not required.
305Shell scripts attempting to be portable to other POSIX systems should use
306the ``[#*]'' convention instead of relying on this behavior.
307The
308.Fl u
309option is an extension to the
310.St -p1003.2
311standard.
312