1TR(P)                      POSIX Programmer's Manual                     TR(P)
2
3
4

NAME

6       tr - translate characters
7

SYNOPSIS

9       tr [-c | -C][-s] string1 string2
10
11       tr -s [-c | -C] string1
12
13       tr -d [-c | -C] string1
14
15       tr -ds [-c | -C] string1 string2
16
17

DESCRIPTION

19       The  tr  utility  shall  copy the standard input to the standard output
20       with substitution or deletion  of  selected  characters.   The  options
21       specified  and  the string1 and string2 operands shall control transla‐
22       tions that occur while copying characters and single-character  collat‐
23       ing elements.
24

OPTIONS

26       The  tr  utility  shall  conform  to  the  Base  Definitions  volume of
27       IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
28
29       The following options shall be supported:
30
31       -c     Complement the set of  values  specified  by  string1.  See  the
32              EXTENDED DESCRIPTION section.
33
34       -C     Complement  the  set of characters specified by string1. See the
35              EXTENDED DESCRIPTION section.
36
37       -d     Delete all occurrences of input characters that are specified by
38              string1.
39
40       -s     Replace  instances  of repeated characters with a single charac‐
41              ter, as described in the EXTENDED DESCRIPTION section.
42
43

OPERANDS

45       The following operands shall be supported:
46
47       string1, string2
48
49              Translation control strings. Each string shall represent  a  set
50              of  characters  to be converted into an array of characters used
51              for the translation. For  a  detailed  description  of  how  the
52              strings are interpreted, see the EXTENDED DESCRIPTION section.
53
54

STDIN

56       The standard input can be any type of file.
57

INPUT FILES

59       None.
60

ENVIRONMENT VARIABLES

62       The following environment variables shall affect the execution of tr:
63
64       LANG   Provide  a  default value for the internationalization variables
65              that are unset or null. (See  the  Base  Definitions  volume  of
66              IEEE Std 1003.1-2001,  Section  8.2,  Internationalization Vari‐
67              ables for the precedence of internationalization variables  used
68              to determine the values of locale categories.)
69
70       LC_ALL If  set  to a non-empty string value, override the values of all
71              the other internationalization variables.
72
73       LC_COLLATE
74
75              Determine the locale for the behavior of range  expressions  and
76              equivalence classes.
77
78       LC_CTYPE
79              Determine  the  locale  for  the  interpretation of sequences of
80              bytes of text data as characters (for  example,  single-byte  as
81              opposed  to multi-byte characters in arguments) and the behavior
82              of character classes.
83
84       LC_MESSAGES
85              Determine the locale that should be used to  affect  the  format
86              and contents of diagnostic messages written to standard error.
87
88       NLSPATH
89              Determine the location of message catalogs for the processing of
90              LC_MESSAGES .
91
92

ASYNCHRONOUS EVENTS

94       Default.
95

STDOUT

97       The tr output shall be identical to the input, with  the  exception  of
98       the specified transformations.
99

STDERR

101       The standard error shall be used only for diagnostic messages.
102

OUTPUT FILES

104       None.
105

EXTENDED DESCRIPTION

107       The  operands  string1  and string2 (if specified) define two arrays of
108       characters. The constructs in the following list can be used to specify
109       characters  or  single-character collating elements. If any of the con‐
110       structs result in multi-character collating elements, tr shall exclude,
111       without a diagnostic, those multi-character elements from the resulting
112       array.
113
114       character
115              Any character not described by  one  of  the  conventions  below
116              shall represent itself.
117
118       \octal Octal  sequences  can  be used to represent characters with spe‐
119              cific coded values. An octal sequence shall consist of  a  back‐
120              slash  followed  by  the longest sequence of one, two, or three-
121              octal-digit characters (01234567). The sequence shall cause  the
122              value  whose  encoding is represented by the one, two, or three-
123              digit octal integer to be placed into the array. If the size  of
124              a byte on the system is greater than nine bits, the valid escape
125              sequence used to represent  a  byte  is  implementation-defined.
126              Multi-byte  characters  require  multiple,  concatenated  escape
127              sequences of this type, including the leading '\' for each byte.
128
129       \character
130              The backslash-escape sequences in the Base Definitions volume of
131              IEEE Std 1003.1-2001, Table 5-1, Escape Sequences and Associated
132              Actions ( '\\' , '\a' , '\b' , '\f' , '\n' , '\r' , '\t' ,  '\v'
133              )  shall be supported. The results of using any other character,
134              other than an octal digit, following the backslash are  unspeci‐
135              fied.
136
137       c-c    In the POSIX locale, this construct shall represent the range of
138              collating elements between the range endpoints (as long as  nei‐
139              ther  endpoint  is an octal sequence of the form \octal), inclu‐
140              sive, as defined by the collation sequence.  The  characters  or
141              collating  elements in the range shall be placed in the array in
142              ascending collation sequence. If the  second  endpoint  precedes
143              the  starting endpoint in the collation sequence, it is unspeci‐
144              fied whether the range of collating elements is empty,  or  this
145              construct is treated as invalid. In locales other than the POSIX
146              locale, this construct has unspecified behavior.
147
148       If either or both of the range endpoints are  octal  sequences  of  the
149       form  \octal,  this  shall represent the range of specific coded values
150       between the two range endpoints, inclusive.
151
152       :class:
153              Represents all characters belonging  to  the  defined  character
154              class,  as defined by the current setting of the LC_CTYPE locale
155              category. The following character class names shall be  accepted
156              when specified in string1:
157
158                       alnum   blank   digit   lower   punct   upper
159                       alpha   cntrl   graph   print   space   xdigit
160
161       In addition, character class expressions of the form [: name:] shall be
162       recognized in those locales where the name keyword  has  been  given  a
163       charclass definition in the LC_CTYPE category.
164
165       When  both  the  -d  and -s options are specified, any of the character
166       class names shall be accepted in  string2.  Otherwise,  only  character
167       class  names  lower  or upper are valid in string2 and then only if the
168       corresponding character class ( upper and lower, respectively) is spec‐
169       ified  in  the  same relative position in string1. Such a specification
170       shall be interpreted as a request for case conversion. When [:  lower:]
171       appears  in string1 and [: upper:] appears in string2, the arrays shall
172       contain the characters from the toupper mapping in the  LC_CTYPE  cate‐
173       gory  of  the current locale. When [: upper:] appears in string1 and [:
174       lower:] appears in string2, the arrays  shall  contain  the  characters
175       from  the  tolower  mapping  in  the  LC_CTYPE  category of the current
176       locale. The first character from each mapping  pair  shall  be  in  the
177       array for string1 and the second character from each mapping pair shall
178       be in the array for string2 in the same relative position.
179
180       Except for case conversion, the characters  specified  by  a  character
181       class expression shall be placed in the array in an unspecified order.
182
183       If the name specified for class does not define a valid character class
184       in the current locale, the behavior is undefined.
185
186       =equiv=
187              Represents all characters or collating elements belonging to the
188              same  equivalence class as equiv, as defined by the current set‐
189              ting of the LC_COLLATE locale  category.  An  equivalence  class
190              expression  shall be allowed only in string1, or in string2 when
191              it is being used by the combined -d and -s options. The  charac‐
192              ters  belonging  to the equivalence class shall be placed in the
193              array in an unspecified order.
194
195       x*n    Represents n repeated occurrences of the character  x.   Because
196              this expression is used to map multiple characters to one, it is
197              only valid when it occurs in string2. If  n  is  omitted  or  is
198              zero,  it  shall  be  interpreted  as large enough to extend the
199              string2-based  sequence  to  the  length  of  the  string1-based
200              sequence. If n has a leading zero, it shall be interpreted as an
201              octal value. Otherwise, it shall be  interpreted  as  a  decimal
202              value.
203
204
205       When the -d option is not specified:
206
207        * Each  input  character found in the array specified by string1 shall
208          be replaced by the character in the same relative  position  in  the
209          array  specified  by string2. When the array specified by string2 is
210          shorter that the one specified by string1, the results are  unspeci‐
211          fied.
212
213        * If  the  -C  option  is specified, the complements of the characters
214          specified by string1 (the set of all characters in the current char‐
215          acter  set,  as  defined by the current setting of LC_CTYPE , except
216          for those actually specified in the string1 operand) shall be placed
217          in the array in ascending collation sequence, as defined by the cur‐
218          rent setting of LC_COLLATE .
219
220        * If the -c option is specified, the complement of the  values  speci‐
221          fied  by  string1 shall be placed in the array in ascending order by
222          binary value.
223
224        * Because the order in which characters specified by  character  class
225          expressions  or  equivalence  class  expressions  is undefined, such
226          expressions should only be used if the  intent  is  to  map  several
227          characters  into  one. An exception is case conversion, as described
228          previously.
229
230       When the -d option is specified:
231
232        * Input characters found in the array specified by  string1  shall  be
233          deleted.
234
235        * When the -C option is specified with -d, all characters except those
236          specified by string1 shall be deleted.  The contents of string2  are
237          ignored, unless the -s option is also specified.
238
239        * When  the  -c  option  is specified with -d, all values except those
240          specified by string1 shall be deleted. The contents of string2 shall
241          be ignored, unless the -s option is also specified.
242
243        * The  same  string  cannot be used for both the -d and the -s option;
244          when both options are specified, both string1  (used  for  deletion)
245          and string2 (used for squeezing) shall be required.
246
247       When  the  -s  option is specified, after any deletions or translations
248       have taken place, repeated sequences of the  same  character  shall  be
249       replaced  by  one occurrence of the same character, if the character is
250       found in the array specified by the last operand. If the  last  operand
251       contains a character class, such as the following example:
252
253
254              tr -s '[:space:]'
255
256       the  last  operand's  array shall contain all of the characters in that
257       character class. However, in a case  conversion,  as  described  previ‐
258       ously, such as:
259
260
261              tr -s '[:upper:]' '[:lower:]'
262
263       the last operand's array shall contain only those characters defined as
264       the second characters in each  of  the  toupper  or  tolower  character
265       pairs, as appropriate.
266
267       An empty string used for string1 or string2 produces undefined results.
268

EXIT STATUS

270       The following exit values shall be returned:
271
272        0     All input was processed successfully.
273
274       >0     An error occurred.
275
276

CONSEQUENCES OF ERRORS

278       Default.
279
280       The following sections are informative.
281

APPLICATION USAGE

283       If necessary, string1 and string2 can be quoted to avoid pattern match‐
284       ing by the shell.
285
286       If an ordinary digit  (representing  itself)  is  to  follow  an  octal
287       sequence,  the  octal  sequence must use the full three digits to avoid
288       ambiguity.
289
290       When string2 is shorter than string1, a difference results between his‐
291       torical  System V  and  BSD systems. A BSD system pads string2 with the
292       last character found in string2.  Thus, it is possible to do  the  fol‐
293       lowing:
294
295
296              tr 0123456789 d
297
298       which would translate all digits to the letter 'd' . Since this area is
299       specifically unspecified in this volume of  IEEE Std 1003.1-2001,  both
300       the  BSD  and System V behaviors are allowed, but a conforming applica‐
301       tion cannot rely on the BSD behavior. It would have to code the example
302       in the following way:
303
304
305              tr 0123456789 '[d*]'
306
307       It should be noted that, despite similarities in appearance, the string
308       operands used by tr are not regular expressions.
309
310       Unlike some historical implementations, this definition of the tr util‐
311       ity correctly processes NUL characters in its input stream. NUL charac‐
312       ters can be stripped by using:
313
314
315              tr -d '\000'
316

EXAMPLES

318        1. The following example creates a list of all words in file1 one  per
319           line in file2, where a word is taken to be a maximal string of let‐
320           ters.
321
322
323           tr -cs "[:alpha:]" "[\n*]" <file1 >file2
324
325        2. The next example translates all lowercase characters  in  file1  to
326           uppercase and writes the results to standard output.
327
328
329           tr "[:lower:]" "[:upper:]" <file1
330
331        3. This  example  uses an equivalence class to identify accented vari‐
332           ants of the base character 'e' in file1, which are stripped of dia‐
333           critical marks and written to file2.
334
335
336           tr "[=e=]" e <file1 >file2
337

RATIONALE

339       In some early proposals, an explicit option -n was added to disable the
340       historical behavior of stripping NUL characters from the input. It  was
341       considered  that  automatically stripping NUL characters from the input
342       was not correct functionality.  However, the removal of -n in  a  later
343       proposal  does not remove the requirement that tr correctly process NUL
344       characters in its input stream. NUL characters can be stripped by using
345       tr -d '\000'.
346
347       Historical  implementations of tr differ widely in syntax and behavior.
348       For example, the BSD version has not needed the bracket characters  for
349       the repetition sequence. The tr utility syntax is based more closely on
350       the System V and XPG3 model while attempting to accommodate  historical
351       BSD  implementations.  In  the  case  of the short string2 padding, the
352       decision was to unspecify the behavior and preserve System V  and  XPG3
353       scripts,  which  might find difficulty with the BSD method. The assump‐
354       tion was made that BSD users of tr have to make accommodations to  meet
355       the  syntax  defined  here.  Since it is possible to use the repetition
356       sequence to duplicate the desired behavior, whereas there is no  simple
357       way to achieve the System V method, this was the correct, if not desir‐
358       able, approach.
359
360       The use of octal values to specify  control  characters,  while  having
361       historical  precedents,  is  not  portable.  The introduction of escape
362       sequences for control characters should provide the necessary portabil‐
363       ity.  It  is  recognized that this may cause some historical scripts to
364       break.
365
366       An early proposal included support for multi-character  collating  ele‐
367       ments.   It was pointed out that, while tr does employ some syntactical
368       elements from REs, the aim of tr is quite different; ranges, for  exam‐
369       ple,  do  not  have  a similar meaning (``any of the chars in the range
370       matches", versus "translate each character in the range to  the  output
371       counterpart").  As a result, the previously included support for multi-
372       character collating elements has been removed. What remains are  ranges
373       in  current  collation order (to support, for example, accented charac‐
374       ters), character classes, and equivalence classes.
375
376       In XPG3 the [: class:] and [= equiv=] conventions are shown with double
377       brackets,  as  in  RE syntax. However, tr does not implement RE princi‐
378       ples; it just borrows part of the syntax. Consequently, [: class:]  and
379       [=  equiv=]  should be regarded as syntactical elements on a par with [
380       x* n], which is not an RE bracket expression.
381
382       The standard developers will consider changes to tr that  allow  it  to
383       translate  characters  between  different  character encodings, or they
384       will consider providing a new utility to accomplish this.
385
386       On historical System V systems, a range expression  requires  enclosing
387       square-brackets, such as:
388
389
390              tr '[a-z]' '[A-Z]'
391
392       However,  BSD-based systems did not require the brackets, and this con‐
393       vention is used here to avoid breaking large numbers of BSD scripts:
394
395
396              tr a-z A-Z
397
398       The preceding System V script will continue to work because the  brack‐
399       ets,  treated as regular characters, are translated to themselves. How‐
400       ever, any System V script that relied on "a-z" representing  the  three
401       characters 'a' , '-' , and 'z' have to be rewritten as "az-" .
402
403       The ISO POSIX-2:1993 standard had a -c option that behaved similarly to
404       the -C option, but did not supply functionality equivalent  to  the  -c
405       option  specified  in IEEE Std 1003.1-2001.  This meant that historical
406       practice of being able to specify tr -d\200-\377  (which  would  delete
407       all  bytes with the top bit set) would have no effect because, in the C
408       locale, bytes with the values octal 200 to octal 377  are  not  charac‐
409       ters.
410
411       The  earlier version also said that octal sequences referred to collat‐
412       ing elements and could be placed adjacent  to  each  other  to  specify
413       multi-byte  characters. However, it was noted that this caused ambigui‐
414       ties because tr would not  be  able  to  tell  whether  adjacent  octal
415       sequences  were  intending to specify multi-byte characters or multiple
416       single byte  characters.   IEEE Std 1003.1-2001  specifies  that  octal
417       sequences always refer to single byte binary values.
418

FUTURE DIRECTIONS

420       None.
421

SEE ALSO

423       sed
424
426       Portions  of  this text are reprinted and reproduced in electronic form
427       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
428       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
429       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
430       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
431       event of any discrepancy between this version and the original IEEE and
432       The  Open Group Standard, the original IEEE and The Open Group Standard
433       is the referee document. The original Standard can be  obtained  online
434       at http://www.opengroup.org/unix/online.html .
435
436
437
438IEEE/The Open Group                  2003                                TR(P)
Impressum