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