⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tr.html

📁 unix 下的C开发手册,还用详细的例程。
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>tr</title></head><body bgcolor=white><center><font size=2>The Single UNIX &reg; Specification, Version 2<br>Copyright &copy; 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_001_014_2334">&nbsp;</a>NAME</h4><blockquote>tr - translate characters</blockquote><h4><a name = "tag_001_014_2335">&nbsp;</a>SYNOPSIS</h4><blockquote><pre><code>tr <b>[</b>-cs<b>]</b><i> string1 string2</i>tr -s<b>[</b>-c<b>]</b><i> string1</i>tr -d<b>[</b>-c<b>]</b><i> string1</i>tr -ds<b>[</b>-c<b>]</b><i> string1 string2</i></code></pre></blockquote><h4><a name = "tag_001_014_2336">&nbsp;</a>DESCRIPTION</h4><blockquote>The<i>tr</i>utility copies the standard input to the standard output withsubstitution or deletion of selected characters.The options specified and the<i>string1</i>and<i>string2</i>operands control translations that occur while copying charactersandsingle-character collating elements.</blockquote><h4><a name = "tag_001_014_2337">&nbsp;</a>OPTIONS</h4><blockquote>The<i>tr</i>utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b>&nbsp;</a> .<p>The following options are supported:<dl compact><dt><b>-c</b><dd>Complement the set of characters specified by<i>string1</i>.See the EXTENDED DESCRIPTION section.<dt><b>-d</b><dd>Delete all occurrences of inputcharacters that are specified by<i>string1</i>.<dt><b>-s</b><dd>Replace instances of repeated characters with a single character,as described in the EXTENDED DESCRIPTION section.</dl></blockquote><h4><a name = "tag_001_014_2338">&nbsp;</a>OPERANDS</h4><blockquote>The following operands are supported:<dl compact><dt><i>string1</i><dd><dt><i>string2</i><dd>Translation control strings.Each string represents a set of characters tobe converted into an array of charactersused for the translation.For a detailed description of how the stringsare interpreted, see the EXTENDED DESCRIPTION section.</dl></blockquote><h4><a name = "tag_001_014_2339">&nbsp;</a>STDIN</h4><blockquote>The standard input can be any type of file.</blockquote><h4><a name = "tag_001_014_2340">&nbsp;</a>INPUT FILES</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_2341">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>The following environment variables affect the execution of<i>tr</i>:<dl compact><dt><i>LANG</i><dd>Provide a default value for the internationalisation variablesthat are unset or null.If<i>LANG</i>is unset or null, the corresponding value from theimplementation-dependent default locale will be used.If any of the internationalisation variables contains an invalid setting, theutility will behave as if none of the variables had been defined.<dt><i>LC_ALL</i><dd>If set to a non-empty string value,override the values of all the other internationalisation variables.<dt><i>LC_COLLATE</i><dd>Determine the locale for thebehaviour of range expressions and equivalence classes.<dt><i>LC_CTYPE</i><dd>Determine thelocale for the interpretation of sequences of bytes of text dataas characters (for example, single-versus multi-byte characters in arguments)and the behaviour of character classes.<dt><i>LC_MESSAGES</i><dd>Determine the locale that should be used to affectthe format and contents of diagnosticmessages written to standard error.<dt><i>NLSPATH</i><dd>Determine the location of message cataloguesfor the processing of<i>LC_MESSAGES .</i></dl></blockquote><h4><a name = "tag_001_014_2342">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_2343">&nbsp;</a>STDOUT</h4><blockquote>The<i>tr</i>output is identical to the input, with the exceptionof the specified transformations.</blockquote><h4><a name = "tag_001_014_2344">&nbsp;</a>STDERR</h4><blockquote>Used only for diagnostic messages.</blockquote><h4><a name = "tag_001_014_2345">&nbsp;</a>OUTPUT FILES</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_2346">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote>The operands<i>string1</i>and<i>string2</i>(if specified)define two arrays of characters.The constructs in the following listcan be used to specify characters or single-character collating elements.If any of the constructs result inmulti-character collating elements,<i>tr</i>will exclude, without a diagnostic,those multi-character elements from the resulting array.<dl compact><dt><i>character</i><dd>Any character not described by one of the conventions belowrepresents itself.<dt><b>\</b><i>octal</i><dd>Octal sequences can be used to represent characterswith specific coded values.An octal sequence consists of a backslashfollowed by thelongest sequence of one-,two- or three-octal-digitcharacters (01234567).The sequence causesthe character whose encodingis represented by theone-, two- or three-digitoctal integer to be placed into the array.If the size of a byte on the system is greater than nine bits,the valid escape sequence used to representa byte is implementation-dependent.Multi-byte characters require multiple,concatenated escape sequences of this type,including the leading \ for each byte.<dt><b>\</b><i>character</i><dd>The backslash-escape sequences inthe table inthe <b>XBD</b> specification, <a href="../xbd/notation.html"><b>File Format Notation</b>&nbsp;</a> (\\,\a,\b,\f,\n,\r,\t,\v)are supported.The results of using any other character,other than an octal digit, following the backslash are unspecified.<dt><i>c-c</i><dd>Represents the range of collating elements between the range endpoints,inclusive,as defined by the current setting of theLC_COLLATE locale category.The starting endpoint must precede the second endpoint inthe current collation order.The characters or collating elements in the rangeare placed in the array in ascending collation sequence.<dt>[:<i>class</i>:]<dd>Represents all characters belonging to the defined character class,as defined by the current setting of theLC_CTYPE locale category.The following character class names will be accepted when specified in<i>string1</i>:<pre><dl compact><dt> <dd><table <tr valign=top><th align=left>alnum<th align=left>blank<th align=left>digit<th align=left>lower<th align=left>punct<th align=left>upper<tr valign=top><td align=left>alpha<td align=left>cntrl<td align=left>graph<td align=left>print<td align=left>space<td align=left>xdigit</table></dl></pre><p>In addition, character class expressions of the form<b>[:</b><i>name</i><b>:]</b>are recognised in those locales where the<i>name</i>keyword has been given a<b>charclass</b>definition in theLC_CTYPE category.<p>When both the<b>-d</b>and<b>-s</b>options are specified,any of the character class names will be accepted in<i>string2</i>.Otherwise, only character class names<b>lower</b>or<b>upper</b>are valid in<i>string2</i>and then only if the corresponding character class(upperand<b>lower</b>,respectively)is specified in the same relative position in<i>string1</i>.Such a specification is interpretedas a request for case conversion.When[:lower:]appears in<i>string1</i>and[:upper:]appears in<i>string2,</i>the arrays will contain the characters from the<b>toupper</b>mapping in the LC_CTYPE category of the current locale.When[:upper:]appears in<i>string1</i>and[:lower:]appears in<i>string2,</i>the arrays will contain the characters from the<b>tolower</b>mapping in the LC_CTYPE category of the current locale.The first character from each mapping pair will be in the array for<i>string1</i>and the second character fromeach mapping pair will be in the array for<i>string2</i>in the same relative position.<p>Except for case conversion, the characters specified by acharacter class expression are placed in the array in anunspecified order.<p>If the name specified for<i>class</i>does not define a validcharacter class in the current locale, the behaviour is undefined.<p><dt>[=<i>equiv</i>=]<dd>Represents all characters or collating elements belonging to thesame equivalence class as<i>equiv ,</i>as defined by the current setting of theLC_COLLATE locale category.An equivalence class expression is allowed only in<i>string1</i>,or in<i>string2</i>when it is being used by the combined<b>-d</b>and<b>-s</b>options.The characters belonging to the equivalence class are placedin the array in an unspecified order.<p><p><dt>[<i>x*n</i>]<dd>Represents<i>n</i>repeated occurrences of the character<i>x .</i>Because this expression is used to map multiple charactersto one, it is only valid when it occurs in<i>string2</i>.If<i>n</i>is omitted or is zero, it is interpreted as large enough to extend the<i>string2</i>-basedsequence to the length of the<i>string1</i>-basedsequence.If<i>n</i>has a leading zero,it is interpreted as an octal value.Otherwise, it is interpreted as a decimal value.<p></dl><p>When the<b>-d</b>option is not specified:<ul><p><li>Each input character found in the array specified by<i>string1</i>is replaced by the character in the samerelative position in the array specified by<i>string2</i>.When the array specified by<i>string2</i>is shorter that the onespecified by<i>string1</i>,the results are unspecified.<p><li>If the<b>-c</b>option is specified,the complements of the characters specified by<i>string1</i>(the set of all characters in the current character set,as defined by the current setting of LC_CTYPE,except for those actually specified in the<i>string1</i>operand)are placed in the array inascending collation sequence, as defined by the current settingof LC_COLLATE.<p><li>Because the order in which characters specified by character classexpressions or equivalence class expressions is undefined,such expressions should only be used if the intent is to mapseveral characters into one.An exception is case conversion,as described previously.<p></ul><p>When the<b>-d</b>option is specified:<ul><p><li>Input characters found in the array specified by<i>string1</i>will be deleted.<p><li>When the<b>-c</b>option is specified with<b>-d</b>,all characters except those specified by<i>string1</i>will be deleted.The contents of<i>string2</i>will be ignored, unless the<b>-s</b>option is also specified.<p><li>The same string cannot be used for both the<b>-d</b>and the<b>-s</b>option; when both options are specified, both<i>string1</i>(used for deletion)and<i>string2</i>(used for squeezing)are required.<p></ul><p>When the<b>-s</b>option is specified,after any deletions ortranslations have taken place,repeated sequences of the samecharacter will be replaced by one occurrence of the same character,if the character is found in the array specified by the last operand.If the last operand contains a character class, such as thefollowing example:<pre><code>tr -s '[:space:]'</code></pre>the last operand's array will contain all of the charactersin that character class.However, in a case conversion, as described previously, such as:<pre><code>tr -s '[:upper:]' '[:lower:]'</code></pre>the last operand's array will contain only those charactersdefined as the second characters in each of the<b>toupper</b>or<b>tolower</b>character pairs, as appropriate.<p>An empty string used for<i>string1</i>or<i>string2</i>produces undefined results.</blockquote><h4><a name = "tag_001_014_2347">&nbsp;</a>EXIT STATUS</h4><blockquote>The following exit values are returned:<dl compact><dt>0<dd>All input was processed successfully.<dt>&gt;0<dd>An error occurred.</dl></blockquote><h4><a name = "tag_001_014_2348">&nbsp;</a>CONSEQUENCES OF ERRORS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_2349">&nbsp;</a>APPLICATION USAGE</h4><blockquote>If necessary,<i>string1</i>and<i>string2</i>can be quoted to avoid pattern matching by the shell.<p>If an ordinary digit (representing itself) is to followan octal sequence, the octal sequence must use thefull three digits to avoid ambiguity.<p>When<i>string2</i>is shorter than<i>string1</i>,a difference results between historical System&nbsp;V and BSD systems.A BSD system will pad<i>string2</i>with the last character found in<i>string2.</i>Thus, it is possible to do the following:<pre><code>tr 0123456789 d</code></pre>which would translate all digits to the letterd.Since this area is specifically unspecified in thedocument, both the BSD and System&nbsp;V behaviours areallowed, but a portable application cannot relyon the BSD behaviour.It would have to code the example in the following way:<pre><code>tr 0123456789 '[d*]'</code></pre><p>It should be noted that, despite similarities in appearance,the string operands used by<i>tr</i>are not regular expressions.<p>Unlike some previous versions, the Issue 4<i>tr</i>correctly processes NUL characters in its input stream.NUL characters can be stripped by using:<pre><code>tr -d '\000'</code></pre><br></blockquote><h4><a name = "tag_001_014_2350">&nbsp;</a>EXAMPLES</h4><blockquote><ol><p><li>The following example creates a list of all words in<i>file1</i>one per line in<i>file2</i>,where a word is taken to be a maximal string of letters.<pre><code>tr -cs "[:alpha:]" "[\n*]" &lt;file1 &gt;file2</code></pre><br><p><li>The next example translates all lower-case characters in<b>file1</b>to upper-case and writes the results to standard output.<pre><code>tr "[:lower:]" "[:upper:]" &lt;file1</code></pre><p>Note that the caveat expressed in the corresponding Issue 3example is no longer in effect.This case conversion is now a special case that employs the<b>tolower</b>and<b>toupper</b>classifications, ensuring that proper mapping is accomplished(when the locale is correctly defined).<p><li>This example uses an equivalence class to identify accented variantsof the base characterein<b>file1</b>,which are stripped of diacritical marks and written to<b>file2</b>.<pre><code>tr "[=e=]" e &lt;file1 &gt;file2</code></pre><p></ol></blockquote><h4><a name = "tag_001_014_2351">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_2352">&nbsp;</a>SEE ALSO</h4><blockquote><i><a href="sed.html">sed</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX &reg; is a registered Trademark of The Open Group.<br>Copyright &copy; 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -