📄 sort.html
字号:
<pre><i>field_number</i><b>[</b><tt>.</tt><i>first_character</i><b>]</b></pre><p>Fields and characters within fields shall be numbered starting with 1. The <i>field_number</i> and <i>first_character</i>pieces, interpreted as positive decimal integers, shall specify the first character to be used as part of a sort key. If<i>.first_character</i> is omitted, it shall refer to the first character of the field.</p><p>The <i>field_end</i> portion of the <i>keydef</i> option-argument shall have the form:</p><pre><i>field_number</i><b>[</b><tt>.</tt><i>last_character</i><b>]</b></pre><p>The <i>field_number</i> shall be as described above for <i>field_start.</i> The <i>last_character</i> piece, interpreted as anon-negative decimal integer, shall specify 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, it shall refer to the last character of the field specified by<i>field_number</i>.</p><p>If the <b>-b</b> option or <b>b</b> type modifier is in effect, characters within a field shall be counted from the first non-<blank> in the field. (This shall apply separately to <i>first_character</i> and <i>last_character</i>.)</p></blockquote><h4><a name="tag_04_131_14"></a>EXIT STATUS</h4><blockquote><p>The following exit values shall be returned:</p><dl compact><dt> 0</dt><dd>All input files were output successfully, or <b>-c</b> was specified and the input file was correctly sorted.</dd><dt> 1</dt><dd>Under the <b>-c</b> option, the file was not ordered as specified, or if the <b>-c</b> and <b>-u</b> options were bothspecified, two input lines were found with equal keys.</dd><dt>>1</dt><dd>An error occurred.</dd></dl></blockquote><h4><a name="tag_04_131_15"></a>CONSEQUENCES OF ERRORS</h4><blockquote><p>Default.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_04_131_16"></a>APPLICATION USAGE</h4><blockquote><p>The default value for <b>-t</b>, <blank>, has different properties from, for example, <b>-t</b> "<space>". If a linecontains:</p><pre><tt><space><space>foo</tt></pre><p>the following treatment would occur with default separation as opposed to specifically selecting a <space>:</p><center><table border="1" cellpadding="3" align="center"><tr valign="top"><th align="center"><p class="tent"><b>Field</b></p></th><th align="center"><p class="tent"><b>Default</b></p></th><th align="center"><p class="tent"><b>-t "<space>"</b></p></th></tr><tr valign="top"><td align="left"><p class="tent">1</p></td><td align="left"><p class="tent"><space><space>foo</p></td><td align="left"><p class="tent"><i>empty</i></p></td></tr><tr valign="top"><td align="left"><p class="tent">2</p></td><td align="left"><p class="tent"><i>empty</i></p></td><td align="left"><p class="tent"><i>empty</i></p></td></tr><tr valign="top"><td align="left"><p class="tent">3</p></td><td align="left"><p class="tent"><i>empty</i></p></td><td align="left"><p class="tent">foo</p></td></tr></table></center><p>The leading field separator itself is included in a field when <b>-t</b> is not used. For example, this command returns an exitstatus of zero, meaning the input was already sorted:</p><pre><tt>sort -c -k 2 <<eofy<tab>bx<space>aeof</tt></pre><p>(assuming that a <tab> precedes the <space> in the current collating sequence). The field separator is not includedin a field when it is explicitly set via <b>-t</b>. This is historical practice and allows usage such as:</p><pre><tt>sort -t "|" -k 2n <<eofAtlanta|425022|GeorgiaBirmingham|284413|AlabamaColumbia|100385|South Carolinaeof</tt></pre><p>where the second field can be correctly sorted numerically without regard to the non-numeric field separator.</p><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 are intended to apply to all specified keys. The way it isdescribed in this volume of IEEE Std 1003.1-2001 matches historical practice, not historical documentation. The resultsare unspecified if these options are specified after a <b>-k</b> option.</p><p>The <b>-f</b> option might not work as expected in locales where there is not a one-to-one mapping between an uppercase and alowercase letter.</p></blockquote><h4><a name="tag_04_131_17"></a>EXAMPLES</h4><blockquote><ol><li><p>The following command sorts the contents of <b>infile</b> with the second field as the sort key:</p><pre><tt>sort -k 2,2 infile</tt></pre></li><li><p>The following command sorts, in reverse order, the contents of <b>infile1</b> and <b>infile2</b>, placing the output in<b>outfile</b> and using the second character of the second field as the sort key (assuming that the first character of the secondfield is the field separator):</p><pre><tt>sort -r -o outfile -k 2.2,2.2 infile1 infile2</tt></pre></li><li><p>The following command sorts the contents of <b>infile1</b> and <b>infile2</b> using the second non- <blank> of the secondfield as the sort key:</p><pre><tt>sort -k 2.2b,2.2b infile1 infile2</tt></pre></li><li><p>The following command prints the System V password file (user database) sorted by the numeric user ID (the thirdcolon-separated field):</p><pre><tt>sort -t : -k 3,3n /etc/passwd</tt></pre></li><li><p>The following command prints the lines of the already sorted file <b>infile</b>, suppressing all but one occurrence of lineshaving the same third field:</p><pre><tt>sort -um -k 3.1,3.0 infile</tt></pre></li></ol></blockquote><h4><a name="tag_04_131_18"></a>RATIONALE</h4><blockquote><p>Examples in some historical documentation state that options <b>-um</b> with one input file keep the first in each set of lineswith equal keys. This behavior was deemed to be an implementation artifact and was not standardized.</p><p>The <b>-z</b> option was omitted; it is not standard practice on most systems and is inconsistent with using <i>sort</i> to sortseveral files individually and then merge them together. The text concerning <b>-z</b> in historical documentation appeared torequire implementations to determine the proper buffer length during the sort phase of operation, but not during the merge.</p><p>The <b>-y</b> option was omitted because of non-portability. The <b>-M</b> option, present in System V, was omitted because ofnon-portability in international usage.</p><p>An undocumented <b>-T</b> option exists in some implementations. It is used to specify a directory for intermediate files.Implementations are encouraged to support the use of the <i>TMPDIR</i> environment variable instead of adding an option to supportthis functionality.</p><p>The <b>-k</b> option was added to satisfy two objections. First, the zero-based counting used by <i>sort</i> is not consistentwith other utility conventions. Second, it did not meet syntax guideline requirements.</p><p>Historical documentation indicates that "setting <b>-n</b> implies <b>-b</b>". The description of <b>-n</b> already statesthat optional leading <blank>s are tolerated in doing the comparison. If <b>-b</b> is enabled, rather than implied, by<b>-n</b>, this has unusual side effects. When a character offset is used in a column of numbers (for example, to sort modulo 100),that offset is measured relative to the most significant digit, not to the column. Based upon a recommendation from the author ofthe original <i>sort</i> utility, the <b>-b</b> implication has been omitted from this volume of IEEE Std 1003.1-2001,and an application wishing to achieve the previously mentioned side effects has to code the <b>-b</b> flag explicitly.</p></blockquote><h4><a name="tag_04_131_19"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_131_20"></a>SEE ALSO</h4><blockquote><p><a href="comm.html"><i>comm</i></a> , <a href="join.html"><i>join</i></a> , <a href="uniq.html"><i>uniq</i></a> , the SystemInterfaces volume of IEEE Std 1003.1-2001, <a href="../functions/toupper.html"><i>toupper</i>()</a></p></blockquote><h4><a name="tag_04_131_21"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 2.</p></blockquote><h4><a name="tag_04_131_22"></a>Issue 6</h4><blockquote><p>IEEE PASC Interpretation 1003.2 #174 is applied, updating the DESCRIPTION of comparisons.</p><p>IEEE PASC Interpretation 1003.2 #168 is applied.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -