logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

tr.1p (19216B)


  1. '\" et
  2. .TH TR "1P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. tr
  12. \(em translate characters
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. tr \fB[\fR-c|-C\fB] [\fR-s\fB] \fIstring1 string2\fR
  17. .P
  18. tr -s \fB[\fR-c|-C\fB] \fIstring1\fR
  19. .P
  20. tr -d \fB[\fR-c|-C\fB] \fIstring1\fR
  21. .P
  22. tr -ds \fB[\fR-c|-C\fB] \fIstring1 string2\fR
  23. .fi
  24. .SH DESCRIPTION
  25. The
  26. .IR tr
  27. utility shall copy the standard input to the standard output with
  28. substitution or deletion of selected characters. The options specified
  29. and the
  30. .IR string1
  31. and
  32. .IR string2
  33. operands shall control translations that occur while copying characters
  34. and single-character collating elements.
  35. .SH OPTIONS
  36. The
  37. .IR tr
  38. utility shall conform to the Base Definitions volume of POSIX.1\(hy2017,
  39. .IR "Section 12.2" ", " "Utility Syntax Guidelines".
  40. .P
  41. The following options shall be supported:
  42. .IP "\fB\-c\fP" 10
  43. Complement the set of values specified by
  44. .IR string1 .
  45. See the EXTENDED DESCRIPTION section.
  46. .IP "\fB\-C\fP" 10
  47. Complement the set of characters specified by
  48. .IR string1 .
  49. See the EXTENDED DESCRIPTION section.
  50. .IP "\fB\-d\fP" 10
  51. Delete all occurrences of input characters that are specified by
  52. .IR string1 .
  53. .IP "\fB\-s\fP" 10
  54. Replace instances of repeated characters with a single character, as
  55. described in the EXTENDED DESCRIPTION section.
  56. .SH OPERANDS
  57. The following operands shall be supported:
  58. .IP "\fIstring1\fR,\ \fIstring2\fR" 10
  59. .br
  60. Translation control strings. Each string shall represent a set of
  61. characters to be converted into an array of characters used for the
  62. translation. For a detailed description of how the strings are
  63. interpreted, see the EXTENDED DESCRIPTION section.
  64. .SH STDIN
  65. The standard input can be any type of file.
  66. .SH "INPUT FILES"
  67. None.
  68. .SH "ENVIRONMENT VARIABLES"
  69. The following environment variables shall affect the execution of
  70. .IR tr :
  71. .IP "\fILANG\fP" 10
  72. Provide a default value for the internationalization variables that are
  73. unset or null. (See the Base Definitions volume of POSIX.1\(hy2017,
  74. .IR "Section 8.2" ", " "Internationalization Variables"
  75. for the precedence of internationalization variables used to determine
  76. the values of locale categories.)
  77. .IP "\fILC_ALL\fP" 10
  78. If set to a non-empty string value, override the values of all the
  79. other internationalization variables.
  80. .IP "\fILC_COLLATE\fP" 10
  81. .br
  82. Determine the locale for the behavior of range expressions and
  83. equivalence classes.
  84. .IP "\fILC_CTYPE\fP" 10
  85. Determine the locale for the interpretation of sequences of bytes of
  86. text data as characters (for example, single-byte as opposed to
  87. multi-byte characters in arguments) and the behavior of character
  88. classes.
  89. .IP "\fILC_MESSAGES\fP" 10
  90. .br
  91. Determine the locale that should be used to affect the format and
  92. contents of diagnostic messages written to standard error.
  93. .IP "\fINLSPATH\fP" 10
  94. Determine the location of message catalogs for the processing of
  95. .IR LC_MESSAGES .
  96. .SH "ASYNCHRONOUS EVENTS"
  97. Default.
  98. .SH STDOUT
  99. The
  100. .IR tr
  101. output shall be identical to the input, with the exception of the
  102. specified transformations.
  103. .SH STDERR
  104. The standard error shall be used only for diagnostic messages.
  105. .SH "OUTPUT FILES"
  106. None.
  107. .SH "EXTENDED DESCRIPTION"
  108. The operands
  109. .IR string1
  110. and
  111. .IR string2
  112. (if specified) define two arrays of characters. The constructs in the
  113. following list can be used to specify characters or single-character
  114. collating elements. If any of the constructs result in multi-character
  115. collating elements,
  116. .IR tr
  117. shall exclude, without a diagnostic, those multi-character elements
  118. from the resulting array.
  119. .IP "\fIcharacter\fR" 10
  120. Any character not described by one of the conventions below shall
  121. represent itself.
  122. .IP "\e\fIoctal\fR" 10
  123. Octal sequences can be used to represent characters with specific coded
  124. values. An octal sequence shall consist of a
  125. <backslash>
  126. followed by the longest sequence of one, two, or three-octal-digit
  127. characters (01234567). The sequence shall cause the value whose encoding
  128. is represented by the one, two, or three-digit octal integer to be placed
  129. into the array. Multi-byte characters require multiple, concatenated
  130. escape sequences of this type, including the leading
  131. <backslash>
  132. for each byte.
  133. .IP "\e\fIcharacter\fR" 10
  134. The
  135. <backslash>-escape
  136. sequences in the Base Definitions volume of POSIX.1\(hy2017,
  137. .IR "Table 5-1" ", " "Escape Sequences and Associated Actions"
  138. (\c
  139. .BR '\e\e' ,
  140. .BR '\ea' ,
  141. .BR '\eb' ,
  142. .BR '\ef' ,
  143. .BR '\en' ,
  144. .BR '\er' ,
  145. .BR '\et' ,
  146. .BR '\ev' )
  147. shall be supported. The results of using any other character, other
  148. than an octal digit, following the
  149. <backslash>
  150. are unspecified. Also, if there is no character following the
  151. <backslash>,
  152. the results are unspecified.
  153. .IP "\fIc\fR\-\fIc\fR" 10
  154. In the POSIX locale, this construct shall represent the range of
  155. collating elements between the range endpoints (as long as neither
  156. endpoint is an octal sequence of the form \e\fIoctal\fP), inclusive, as
  157. defined by the collation sequence. The characters or collating elements
  158. in the range shall be placed in the array in ascending collation
  159. sequence. If the second endpoint precedes the starting endpoint in the
  160. collation sequence, it is unspecified whether the range of collating
  161. elements is empty, or this construct is treated as invalid. In locales
  162. other than the POSIX locale, this construct has unspecified behavior.
  163. .RS 10
  164. .P
  165. If either or both of the range endpoints are octal sequences of the
  166. form \e\fIoctal\fP, this shall represent the range of specific coded
  167. values between the two range endpoints, inclusive.
  168. .RE
  169. .IP "[:\fIclass\fR:]" 10
  170. Represents all characters belonging to the defined character class, as
  171. defined by the current setting of the
  172. .IR LC_CTYPE
  173. locale category. The following character class names shall be accepted
  174. when specified in
  175. .IR string1 :
  176. .TS
  177. tab(@);
  178. lB lB lB lB lB lB.
  179. alnum@blank@digit@lower@punct@upper
  180. alpha@cntrl@graph@print@space@xdigit
  181. .TE
  182. .RS 10
  183. .P
  184. In addition, character class expressions of the form [:\c
  185. .IR name :]
  186. shall be recognized in those locales where the
  187. .IR name
  188. keyword has been given a
  189. .BR charclass
  190. definition in the
  191. .IR LC_CTYPE
  192. category.
  193. .P
  194. When both the
  195. .BR \-d
  196. and
  197. .BR \-s
  198. options are specified, any of the character class names shall be
  199. accepted in
  200. .IR string2 .
  201. Otherwise, only character class names
  202. .BR lower
  203. or
  204. .BR upper
  205. are valid in
  206. .IR string2
  207. and then only if the corresponding character class (\c
  208. .BR upper
  209. and
  210. .BR lower ,
  211. respectively) is specified in the same relative position in
  212. .IR string1 .
  213. Such a specification shall be interpreted as a request for case
  214. conversion. When [:\c
  215. .IR lower :]
  216. appears in
  217. .IR string1
  218. and [:\c
  219. .IR upper :]
  220. appears in
  221. .IR string2 ,
  222. the arrays shall contain the characters from the
  223. .BR toupper
  224. mapping in the
  225. .IR LC_CTYPE
  226. category of the current locale. When [:\c
  227. .IR upper :]
  228. appears in
  229. .IR string1
  230. and [:\c
  231. .IR lower :]
  232. appears in
  233. .IR string2 ,
  234. the arrays shall contain the characters from the
  235. .BR tolower
  236. mapping in the
  237. .IR LC_CTYPE
  238. category of the current locale. The first character from each mapping
  239. pair shall be in the array for
  240. .IR string1
  241. and the second character from each mapping pair shall be in the array
  242. for
  243. .IR string2
  244. in the same relative position.
  245. .P
  246. Except for case conversion, the characters specified by a character
  247. class expression shall be placed in the array in an unspecified order.
  248. .P
  249. If the name specified for
  250. .IR class
  251. does not define a valid character class in the current locale, the
  252. behavior is undefined.
  253. .RE
  254. .IP "[=\fIequiv\fR=]" 10
  255. Represents all characters or collating elements belonging to the same
  256. equivalence class as
  257. .IR equiv ,
  258. as defined by the current setting of the
  259. .IR LC_COLLATE
  260. locale category. An equivalence class expression shall be allowed only
  261. in
  262. .IR string1 ,
  263. or in
  264. .IR string2
  265. when it is being used by the combined
  266. .BR \-d
  267. and
  268. .BR \-s
  269. options. The characters belonging to the equivalence class shall be
  270. placed in the array in an unspecified order.
  271. .IP "[\fIx\fR*\fIn\fR]" 10
  272. Represents
  273. .IR n
  274. repeated occurrences of the character
  275. .IR x .
  276. Because this expression is used to map multiple characters to one, it
  277. is only valid when it occurs in
  278. .IR string2 .
  279. If
  280. .IR n
  281. is omitted or is zero, it shall be interpreted as large enough to
  282. extend the
  283. .IR string2 -based
  284. sequence to the length of the
  285. .IR string1 -based
  286. sequence. If
  287. .IR n
  288. has a leading zero, it shall be interpreted as an octal value.
  289. Otherwise, it shall be interpreted as a decimal value.
  290. .P
  291. When the
  292. .BR \-d
  293. option is not specified:
  294. .IP " *" 4
  295. If
  296. .IR string2
  297. is present, each input character found in the array specified by
  298. .IR string1
  299. shall be replaced by the character in the same relative position in the
  300. array specified by
  301. .IR string2 .
  302. If the array specified by
  303. .IR string2
  304. is shorter that the one specified by
  305. .IR string1 ,
  306. or if a character occurs more than once in
  307. .IR string1 ,
  308. the results are unspecified.
  309. .IP " *" 4
  310. If the
  311. .BR \-C
  312. option is specified, the complements of the characters specified by
  313. .IR string1
  314. (the set of all characters in the current character set, as defined by
  315. the current setting of
  316. .IR LC_CTYPE ,
  317. except for those actually specified in the
  318. .IR string1
  319. operand) shall be placed in the array in ascending collation sequence,
  320. as defined by the current setting of
  321. .IR LC_COLLATE .
  322. .IP " *" 4
  323. If the
  324. .BR \-c
  325. option is specified, the complement of the values specified by
  326. .IR string1
  327. shall be placed in the array in ascending order by binary value.
  328. .IP " *" 4
  329. Because the order in which characters specified by character class
  330. expressions or equivalence class expressions is undefined, such
  331. expressions should only be used if the intent is to map several
  332. characters into one. An exception is case conversion, as described
  333. previously.
  334. .P
  335. When the
  336. .BR \-d
  337. option is specified:
  338. .IP " *" 4
  339. Input characters found in the array specified by
  340. .IR string1
  341. shall be deleted.
  342. .IP " *" 4
  343. When the
  344. .BR \-C
  345. option is specified with
  346. .BR \-d ,
  347. all characters except those specified by
  348. .IR string1
  349. shall be deleted. The contents of
  350. .IR string2
  351. are ignored, unless the
  352. .BR \-s
  353. option is also specified.
  354. .IP " *" 4
  355. When the
  356. .BR \-c
  357. option is specified with
  358. .BR \-d ,
  359. all values except those specified by
  360. .IR string1
  361. shall be deleted. The contents of
  362. .IR string2
  363. shall be ignored, unless the
  364. .BR \-s
  365. option is also specified.
  366. .IP " *" 4
  367. The same string cannot be used for both the
  368. .BR \-d
  369. and the
  370. .BR \-s
  371. option; when both options are specified, both
  372. .IR string1
  373. (used for deletion) and
  374. .IR string2
  375. (used for squeezing) shall be required.
  376. .P
  377. When the
  378. .BR \-s
  379. option is specified, after any deletions or translations have taken
  380. place, repeated sequences of the same character shall be replaced by
  381. one occurrence of the same character, if the character is found in the
  382. array specified by the last operand. If the last operand contains a
  383. character class, such as the following example:
  384. .sp
  385. .RS 4
  386. .nf
  387. tr -s \(aq[:space:]\(aq
  388. .fi
  389. .P
  390. .RE
  391. .P
  392. the last operand's array shall contain all of the characters in that
  393. character class. However, in a case conversion, as described
  394. previously, such as:
  395. .sp
  396. .RS 4
  397. .nf
  398. tr -s \(aq[:upper:]\(aq \(aq[:lower:]\(aq
  399. .fi
  400. .P
  401. .RE
  402. .P
  403. the last operand's array shall contain only those characters defined as
  404. the second characters in each of the
  405. .BR toupper
  406. or
  407. .BR tolower
  408. character pairs, as appropriate.
  409. .P
  410. An empty string used for
  411. .IR string1
  412. or
  413. .IR string2
  414. produces undefined results.
  415. .SH "EXIT STATUS"
  416. The following exit values shall be returned:
  417. .IP "\00" 6
  418. All input was processed successfully.
  419. .IP >0 6
  420. An error occurred.
  421. .SH "CONSEQUENCES OF ERRORS"
  422. Default.
  423. .LP
  424. .IR "The following sections are informative."
  425. .SH "APPLICATION USAGE"
  426. If necessary,
  427. .IR string1
  428. and
  429. .IR string2
  430. can be quoted to avoid pattern matching by the shell.
  431. .P
  432. If an ordinary digit (representing itself) is to follow an octal
  433. sequence, the octal sequence must use the full three digits to avoid
  434. ambiguity.
  435. .P
  436. When
  437. .IR string2
  438. is shorter than
  439. .IR string1 ,
  440. a difference results between historical System\ V and BSD systems. A
  441. BSD system pads
  442. .IR string2
  443. with the last character found in
  444. .IR string2 .
  445. Thus, it is possible to do the following:
  446. .sp
  447. .RS 4
  448. .nf
  449. tr 0123456789 d
  450. .fi
  451. .P
  452. .RE
  453. .P
  454. which would translate all digits to the letter
  455. .BR 'd' .
  456. Since this area is specifically unspecified in this volume of POSIX.1\(hy2017, both the BSD and
  457. System\ V behaviors are allowed, but a conforming application cannot rely
  458. on the BSD behavior. It would have to code the example in the
  459. following way:
  460. .sp
  461. .RS 4
  462. .nf
  463. tr 0123456789 \(aq[d*]\(aq
  464. .fi
  465. .P
  466. .RE
  467. .P
  468. It should be noted that, despite similarities in appearance, the string
  469. operands used by
  470. .IR tr
  471. are not regular expressions.
  472. .P
  473. Unlike some historical implementations, this definition of the
  474. .IR tr
  475. utility correctly processes NUL characters in its input stream. NUL
  476. characters can be stripped by using:
  477. .sp
  478. .RS 4
  479. .nf
  480. tr -d \(aq\e000\(aq
  481. .fi
  482. .P
  483. .RE
  484. .SH EXAMPLES
  485. .IP " 1." 4
  486. The following example creates a list of all words in
  487. .BR file1
  488. one per line in
  489. .BR file2 ,
  490. where a word is taken to be a maximal string of letters.
  491. .RS 4
  492. .sp
  493. .RS 4
  494. .nf
  495. tr -cs "[:alpha:]" "[\en*]" <file1 >file2
  496. .fi
  497. .P
  498. .RE
  499. .RE
  500. .IP " 2." 4
  501. The next example translates all lowercase characters in
  502. .BR file1
  503. to uppercase and writes the results to standard output.
  504. .RS 4
  505. .sp
  506. .RS 4
  507. .nf
  508. tr "[:lower:]" "[:upper:]" <file1
  509. .fi
  510. .P
  511. .RE
  512. .RE
  513. .IP " 3." 4
  514. This example uses an equivalence class to identify accented variants of
  515. the base character
  516. .BR 'e'
  517. in
  518. .BR file1 ,
  519. which are stripped of diacritical marks and written to
  520. .BR file2 .
  521. .RS 4
  522. .sp
  523. .RS 4
  524. .nf
  525. tr "[=e=]" "[e*]" <file1 >file2
  526. .fi
  527. .P
  528. .RE
  529. .RE
  530. .SH RATIONALE
  531. In some early proposals, an explicit option
  532. .BR \-n
  533. was added to disable the historical behavior of stripping NUL
  534. characters from the input. It was considered that automatically
  535. stripping NUL characters from the input was not correct functionality.
  536. However, the removal of
  537. .BR \-n
  538. in a later proposal does not remove the requirement that
  539. .IR tr
  540. correctly process NUL characters in its input stream. NUL characters
  541. can be stripped by using
  542. .IR tr
  543. .BR \-d
  544. \&\(aq\e000\(aq.
  545. .P
  546. Historical implementations of
  547. .IR tr
  548. differ widely in syntax and behavior. For example, the BSD version has
  549. not needed the bracket characters for the repetition sequence. The
  550. .IR tr
  551. utility syntax is based more closely on the System V and XPG3 model
  552. while attempting to accommodate historical BSD implementations. In the
  553. case of the short
  554. .IR string2
  555. padding, the decision was to unspecify the behavior and preserve System
  556. V and XPG3 scripts, which might find difficulty with the BSD method.
  557. The assumption was made that BSD users of
  558. .IR tr
  559. have to make accommodations to meet the syntax defined here. Since it
  560. is possible to use the repetition sequence to duplicate the desired
  561. behavior, whereas there is no simple way to achieve the System V
  562. method, this was the correct, if not desirable, approach.
  563. .P
  564. The use of octal values to specify control characters, while having
  565. historical precedents, is not portable. The introduction of escape
  566. sequences for control characters should provide the necessary
  567. portability. It is recognized that this may cause some historical
  568. scripts to break.
  569. .P
  570. An early proposal included support for multi-character collating elements.
  571. It was pointed out that, while
  572. .IR tr
  573. does employ some syntactical elements from REs, the aim of
  574. .IR tr
  575. is quite different; ranges, for example, do not have a similar meaning
  576. (``any of the chars in the range matches'', \fIversus\fP ``translate
  577. each character in the range to the output counterpart''). As a result,
  578. the previously included support for multi-character collating elements
  579. has been removed. What remains are ranges in current collation order
  580. (to support, for example, accented characters), character classes, and
  581. equivalence classes.
  582. .P
  583. In XPG3 the [:\c
  584. .IR class :]
  585. and [=\c
  586. .IR equiv =]
  587. conventions are shown with double brackets, as in RE syntax. However,
  588. .IR tr
  589. does not implement RE principles; it just borrows part of the syntax.
  590. Consequently, [:\c
  591. .IR class :]
  592. and [=\c
  593. .IR equiv =]
  594. should be regarded as syntactical elements on a par with [\c
  595. .IR x *\c
  596. .IR n ],
  597. which is not an RE bracket expression.
  598. .P
  599. The standard developers will consider changes to
  600. .IR tr
  601. that allow it to translate characters between different character
  602. encodings, or they will consider providing a new utility to accomplish
  603. this.
  604. .P
  605. On historical System V systems, a range expression requires enclosing
  606. square-brackets, such as:
  607. .sp
  608. .RS 4
  609. .nf
  610. tr \(aq[a-z]\(aq \(aq[A-Z]\(aq
  611. .fi
  612. .P
  613. .RE
  614. .P
  615. However, BSD-based systems did not require the brackets, and this
  616. convention is used here to avoid breaking large numbers of BSD scripts:
  617. .sp
  618. .RS 4
  619. .nf
  620. tr a-z A-Z
  621. .fi
  622. .P
  623. .RE
  624. .P
  625. The preceding System V script will continue to work because the
  626. brackets, treated as regular characters, are translated to themselves.
  627. However, any System V script that relied on
  628. .BR \(dqa\(hyz\(dq
  629. representing the three characters
  630. .BR 'a' ,
  631. .BR '\-' ,
  632. and
  633. .BR 'z'
  634. have to be rewritten as
  635. .BR \(dqaz-\(dq .
  636. .P
  637. The ISO\ POSIX\(hy2:\|1993 standard had a
  638. .BR \-c
  639. option that behaved similarly to the
  640. .BR \-C
  641. option, but did not supply functionality equivalent to the
  642. .BR \-c
  643. option specified in POSIX.1\(hy2008.
  644. .P
  645. The earlier version also said that octal sequences referred to
  646. collating elements and could be placed adjacent to each other to
  647. specify multi-byte characters. However, it was noted that this caused
  648. ambiguities because
  649. .IR tr
  650. would not be able to tell whether adjacent octal sequences were
  651. intending to specify multi-byte characters or multiple single byte
  652. characters. POSIX.1\(hy2008 specifies that octal sequences always refer to single
  653. byte binary values when used to specify an endpoint of a range of
  654. collating elements.
  655. .P
  656. Earlier versions of this standard allowed for implementations with
  657. bytes other than eight bits, but this has been modified in this
  658. version.
  659. .SH "FUTURE DIRECTIONS"
  660. None.
  661. .SH "SEE ALSO"
  662. .IR "\fIsed\fR\^"
  663. .P
  664. The Base Definitions volume of POSIX.1\(hy2017,
  665. .IR "Table 5-1" ", " "Escape Sequences and Associated Actions",
  666. .IR "Chapter 8" ", " "Environment Variables",
  667. .IR "Section 12.2" ", " "Utility Syntax Guidelines"
  668. .\"
  669. .SH COPYRIGHT
  670. Portions of this text are reprinted and reproduced in electronic form
  671. from IEEE Std 1003.1-2017, Standard for Information Technology
  672. -- Portable Operating System Interface (POSIX), The Open Group Base
  673. Specifications Issue 7, 2018 Edition,
  674. Copyright (C) 2018 by the Institute of
  675. Electrical and Electronics Engineers, Inc and The Open Group.
  676. In the event of any discrepancy between this version and the original IEEE and
  677. The Open Group Standard, the original IEEE and The Open Group Standard
  678. is the referee document. The original Standard can be obtained online at
  679. http://www.opengroup.org/unix/online.html .
  680. .PP
  681. Any typographical or formatting errors that appear
  682. in this page are most likely
  683. to have been introduced during the conversion of the source files to
  684. man page format. To report such errors, see
  685. https://www.kernel.org/doc/man-pages/reporting_bugs.html .