📄 sort.html
字号:
<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_2038"> </a>ASYNCHRONOUS EVENTS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_2039"> </a>STDOUT</h4><blockquote>Unless the<b>-o</b>or<b>-c</b>options are in effect, the standard output contains the sorted input.</blockquote><h4><a name = "tag_001_014_2040"> </a>STDERR</h4><blockquote>Used for diagnostic messages.A warning message about correcting an incomplete last lineof an input file may be generated, but need not affect thefinal exit status.</blockquote><h4><a name = "tag_001_014_2041"> </a>OUTPUT FILES</h4><blockquote>If the<b>-o</b>option is in effect, the sorted input is placed in the file<i>output</i>.</blockquote><h4><a name = "tag_001_014_2042"> </a>EXTENDED DESCRIPTION</h4><blockquote>The notation:<pre><code>-k <i>field_start</i><b>[</b><i>type</i><b>][</b>,<i>field_end</i><b>[</b><i>type</i><b>]]</b></code></pre>defines a key field that begins at<i>field_start</i>and ends at<i>field_end</i>inclusive, unless<i>field_start</i>falls beyond the end of the line or after<i>field_end</i>,in which case the key field is empty.A missing<i>field_end</i>means the last character of the line.<p>A field comprises a maximal sequenceof non-separating characters and,in the absence of option<b>-t</b>,any preceding field separator.<p>The<i>field_start</i>portion of the<i>keydef</i>option-argument has the form:<pre><code><i>field_number</i><b>[</b>.<i>first_character</i><b>]</b></code></pre><p>Fields and characters within fields are numbered starting with 1.The<i>field_number</i>and<i>first_character</i>pieces, interpreted as positivedecimal integers, specify the first character to be used as part ofa sort key.If<i>.first_character</i>is omitted, it refers to the first character of the field.<p>The<i>field_end</i>portion of the<i>keydef</i>option-argument has the form:<pre><code><i>field_number</i><b>[</b>.<i>last_character</i><b>]</b></code></pre><p>The<i>field_number</i>is as described above for<i>field_start.</i>The<i>last_character</i>piece, interpreted as a non-negative decimal integer,specifies the last character to be used as part of the sort key.If<i>last_character</i>evaluates to zero or<i>.last_character</i>is omitted, itrefers to the last character of the field specified by<i>field_number.</i><p>If the<b>-b</b>option orbtype modifier is in effect, characters within afield are counted from the firstnon-blank characterin the field.(This applies separately to<i>first_character</i>and<i>last_character</i>.)<p>The obsolescent options:<pre><code><b>[</b>+<i>pos1</i><b>[</b>-<i>pos2</i><b>]]</b></code></pre>provide functionality equivalent to the<b>-k</b> <i>keydef</i>option.For comparison, the full formats of these options are:<pre><code>+<i>field0_number</i><b>[</b>.<i>first0_character</i><b>][</b><i>type</i><b>] [</b>-<i>field0_number</i><b>[</b>.<i>first0_character</i><b>][</b><i>type</i><b>]]</b>-k <i>field_number</i><b>[</b>.<i>first_character</i><b>][</b><i>type</i><b>] [</b>,<i>field_number</i><b>[</b>.<i>last_character</i><b>][</b><i>type</i><b>]]</b></code></pre><p>In the obsolescent form, fields (specified by<i>field0_number</i>)and characters within fields (specified by<i>first0_character</i>)are numbered from zero instead of one.The optional type modifiers are the same in both forms.If<i>.first0_character</i>is omitted or<i>first0_character</i>evaluates to zero, it refers to the first character of the field.The<b>-b</b>option does not apply to<i>-pos2</i>.<p>The fully specified<i>+pos1</i><i>-pos2</i>form with type modifiers<i>T</i>and<i>U</i>:<pre><code>+<i>w</i>.<i>xT</i> -<i>y</i>.<i>zU</i></code></pre><br>is equivalent to:<pre><dl compact><dt> <dd><table <tr valign=top><th align=left><i>undefined</i><th align=left>(<i>z</i>==0 & <i>U</i> contains b & -t is present)<tr valign=top><td align=left>-k <i>w</i>+1.<i>x</i>+1<i>T</i>,<i>y</i>.0<i>U</i><td align=left>(<i>z</i>==0 otherwise)<tr valign=top><td align=left>-k <i>w</i>+1.<i>x</i>+1<i>T</i>,<i>y</i>+1.<i>zU</i><td align=left>(<i>z</i> > 0)</table></dl></pre><p>As with the non-obsolescent forms, implementations support at leastnine occurrences of the<i>+pos1</i>option, which are significant in command line order.</blockquote><h4><a name = "tag_001_014_2043"> </a>EXIT STATUS</h4><blockquote>The following exit values are returned:<dl compact><dt>0<dd>All input files were output successfully,or<b>-c</b>was specified and the input file was correctly sorted.<dt>1<dd>Under the<b>-c</b>option, the file was not orderedas specified, orif the<b>-c</b>and<b>-u</b>options were both specified,two input lines were found with equal keys.<dt>>1<dd>An error occurred.</dl></blockquote><h4><a name = "tag_001_014_2044"> </a>CONSEQUENCES OF ERRORS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_2045"> </a>APPLICATION USAGE</h4><blockquote>The default value for<b>-t</b>,blank character,has different properties from, for example,-t "<space>".If a line contains:<pre><code><space><space>foo</code></pre>the following treatment would occurwith default separation as opposed to specifically selecting aspace character:<br><p><table bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Field</b><th align=center><b>Default</b><th align=center><b>-t "<space>"</b><tr valign=top><td align=left>1<td align=left><space><space>foo<td align=left><i>empty</i><tr valign=top><td align=left>2<td align=left><i>empty</i><td align=left><i>empty</i><tr valign=top><td align=left>3<td align=left><i>empty</i><td align=left>foo</table><p>The leading field separator itself is included in a field when<b>-t</b>is not used.For example, this command returns an exit statusof zero, meaning the input was already sorted:<pre><code>sort -c -k 2 <<eofy<tab>bx<space>aeof</code></pre>(assuming that atab characterprecedes thespace characterin the current collating sequence).The field separator is not included in a field when itis explicitly set via<b>-t</b>.This is historical practice and allows usage such as:<pre><code>sort -t "|" -k 2n <<eofAtlanta|425022|GeorgiaBirmingham|284413|AlabamaColumbia|100385|South Carolinaeof</code></pre>where the second field can be correctly sorted numericallywithout regard to the non-numeric field separator.<p>The wording in the OPTIONS section clarifies that the<b>-b</b>,<b>-d</b>,<b>-f</b>,<b>-i</b>,<b>-n</b>and<b>-r</b>options have to come before the first sort key specified if they areintended to apply to all specified keys.The way it is described in thisdocument matches historical practice, not historical documentation.In the non-obsolescent versions, the results are unspecified if these optionsare specified after a<b>-k</b>option.<p>The<b>-f</b>option might not work as expected in locales where there is not aone-to-one mapping between an upper- and a lower-case letter.</blockquote><h4><a name = "tag_001_014_2046"> </a>EXAMPLES</h4><blockquote>In the following examples, non-obsolescent and obsolescent ways of specifyingsort keys are given as an aid to understanding the relationshipbetween the two forms.<ol><p><li>Either of the following commands sorts the contents of<b>infile</b>with the second field as the sort key:<pre><code> sort -k 2,2 infile sort +1 -2 infile.sE</code></pre><p><li>Either of the following commands sorts, in reverse order, the contents of<b>infile1</b>and<b>infile2</b>,placing the output in<b>outfile</b>and using the secondcharacter of the second field as the sort key(assuming that the first character of thesecond field is the field separator):<pre><code> sort -r -o outfile -k 2.2,2.2 infile1 infile2 sort -r -o outfile +1.1 -1.2 infile1 infile2.sE</code></pre><p><li>Either of the following commands sorts the contents of<b>infile1</b>and<b>infile2</b>using the secondnon-blankcharacter of the secondfield as the sort key:<pre><code> sort -k 2.2b,2.2b infile1 infile2 sort +1.1b -1.2b infile1 infile2.sE</code></pre><p><li>Either of the following commands prints the System V password file (userdatabase) sorted by the numeric user ID (the third colon-separatedfield):<pre><code> sort -t : -k 3,3n /etc/passwd sort -t : +2 -3n /etc/passwd.sE</code></pre><p><li>Either of the following commands prints the lines of the already sortedfile<b>infile</b>,suppressing all but one occurrence of lines having thesame third field:<pre><code> sort -um -k 3.1,3.0 infile sort -um +2.0 -3.0 infile.sE</code></pre><p></ol></blockquote><h4><a name = "tag_001_014_2047"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_2048"> </a>SEE ALSO</h4><blockquote><i><a href="comm.html">comm</a></i>,<i><a href="join.html">join</a></i>,<i><a href="uniq.html">uniq</a></i>,the <b>XSH</b> specification description of<i><a href="../xsh/toupper.html">toupper()</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 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 + -