The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
<html><head><title>sort</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" >
</head>
<body class='pod'>
<!--
  generated by Pod::Simple::HTML v3.01,
  using Pod::Simple::PullParser v2.02,
  under Perl v5.008002 at Thu Aug  5 13:56:22 2004 GMT.

 If you want to change this HTML document, you probably shouldn't do that
   by changing it directly.  Instead, see about changing the calling options
   to Pod::Simple::HTML, and/or subclassing Pod::Simple::HTML,
   then reconverting this document from the Pod source.
   When in doubt, email the author of Pod::Simple::HTML for advice.
   See 'perldoc Pod::Simple::HTML' for more info.

-->

<!-- start doc -->
<a name='___top' class='dummyTopAnchor' ></a>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="NAME"
>NAME</a></h1>

<p>sort - sort or merge text files</p>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="SYNOPSIS"
>SYNOPSIS</a></h1>

<pre lang='und' xml:lang='und'>  sort [-cmudfinrbD] [-o output_file]
    [-t field_separator] [-X regex_field_separator] [-R record_separator]
    [-k pos1[,pos2]] [+pos1 [-pos2]]
    [-y max_records] [-F max_files]
    input_file1 [input_file2 ...]

  See docs for more information.</pre>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="DESCRIPTION"
>DESCRIPTION</a></h1>

<p>The sort utility sorts text files by lines (or records). Comparisons are based on one or more sort keys extracted from each line of input, and are performed lexicographically. By default, if keys are not given, sort regards each input line as a single field. The sort is a merge sort. If you don&#39;t like that, feel free to change it.</p>

<h2><a class='u' href='#___top' title='click to go to top of document'
name="Options"
>Options</a></h2>

<p>The following options are available:</p>

<dl>
<dt><a name="I_INPUT"
><code lang='und' xml:lang='und'>I</code> <i>INPUT</i></a></dt><p class="pad"></p>

<dd>Pass in the input file(s). This can be either a single string with the filename, or an array reference containing multiple filename strings.</dd><p class="pad"></p>

<dt><a name="c"
><code lang='und' xml:lang='und'>c</code></a></dt><p class="pad"></p>

<dd>Check that single input fle is ordered as specified by the arguments and the collating sequence of the current locale. No output is produced; only the exit code is affected.</dd><p class="pad"></p>

<dt><a name="m"
><code lang='und' xml:lang='und'>m</code></a></dt><p class="pad"></p>

<dd>Merge only; the input files are assumed to already be sorted.</dd><p class="pad"></p>

<dt><a name="o_OUTPUT"
><code lang='und' xml:lang='und'>o</code> <i>OUTPUT</i></a></dt><p class="pad"></p>

<dd>Specify the name of an <i>OUTPUT</i> file to be used instead of the standard output.</dd><p class="pad"></p>

<dt><a name="u"
><code lang='und' xml:lang='und'>u</code></a></dt><p class="pad"></p>

<dd>Unique: Suppresses all but one in each set of lines having equal keys. If used with the <b>c</b> option check that there are no lines with consecutive lines with duplicate keys, in addition to checking that the input file is sorted.</dd><p class="pad"></p>

<dt><a name="y_MAX_SORT_RECORDS"
><code lang='und' xml:lang='und'>y</code> <i>MAX_SORT_RECORDS</i></a></dt><p class="pad"></p>

<dd>Maximum number of lines (records) read before writing to temp file. Default is 200,000. This may eventually change to be kbytes instead of lines. Lines was easier to implement. Can also specify with MAX_SORT_RECORDS environment variable.</dd><p class="pad"></p>

<dt><a name="F_MAX_SORT_FILES"
><code lang='und' xml:lang='und'>F</code> <i>MAX_SORT_FILES</i></a></dt><p class="pad"></p>

<dd>Maximum number of temp files to be held open at once. Default to 40, as older Windows ports had quite a small limit. Can also specify with MAX_SORT_FILES environment variable. No temp files will be used at all if MAX_SORT_RECORDS is never reached.</dd><p class="pad"></p>

<dt><a name="D"
><code lang='und' xml:lang='und'>D</code></a></dt><p class="pad"></p>

<dd>Send debugging information to STDERR. Behavior subject to change.</dd><p class="pad"></p>
</dl>

<p>The following options override the default ordering rules. When ordering options appear independent of any key field specifications, the requested field ordering rules are applied globally to all sort keys. When attached to a specific key (see <b>k</b>), the specified ordering options override all global ordering options for that key.</p>

<dl>
<dt><a name="d"
><code lang='und' xml:lang='und'>d</code></a></dt><p class="pad"></p>

<dd>Specify that only blank characters and alphanumeric characters, according to the current locale setting, are significant in comparisons. <b>d</b> overrides <b>i</b>.</dd><p class="pad"></p>

<dt><a name="f"
><code lang='und' xml:lang='und'>f</code></a></dt><p class="pad"></p>

<dd>Consider all lower-case characters that have upper-case equivalents, according to the current locale setting, to be the upper-case equivalent for the purposes of comparison.</dd><p class="pad"></p>

<dt><a name="i"
><code lang='und' xml:lang='und'>i</code></a></dt><p class="pad"></p>

<dd>Ignores all characters that are non-printable, according to the current locale setting.</dd><p class="pad"></p>

<dt><a name="n"
><code lang='und' xml:lang='und'>n</code></a></dt><p class="pad"></p>

<dd>Does numeric instead of string compare, using whatever perl considers to be a number in numeric comparisons.</dd><p class="pad"></p>

<dt><a name="r"
><code lang='und' xml:lang='und'>r</code></a></dt><p class="pad"></p>

<dd>Reverse the sense of the comparisons.</dd><p class="pad"></p>

<dt><a name="b"
><code lang='und' xml:lang='und'>b</code></a></dt><p class="pad"></p>

<dd>Ignore leading blank characters when determining the starting and ending positions of a restricted sort key. If the <b>b</b> option is specified before the first <b>k</b> option, it is applied to all <b>k</b> options. Otherwise, the <b>b</b> option can be attached indepently to each field_start or field_end option argument (see below).</dd><p class="pad"></p>

<dt><a name="t_STRING"
><code lang='und' xml:lang='und'>t</code> <i>STRING</i></a></dt><p class="pad"></p>

<dd>Use <i>STRING</i> as the field separator character; char is not considered to be part of a field (although it can be included in a sort key). Each occurrence of char is significant (for example, &#60;char&#62;&#60;char&#62; delimits an empty field). If <b>t</b> is not specified, blank characters are used as default field separators; each maximal non-empty sequence of blank characters that follows a non-blank character is a field separator.</dd><p class="pad"></p>

<dt><a name="X_STRING"
><code lang='und' xml:lang='und'>X</code> <i>STRING</i></a></dt><p class="pad"></p>

<dd>Same as <b>t</b>, but <i>STRING</i> is interpreted as a Perl regular expression instead. Do not escape any characters (<code lang='und' xml:lang='und'>/</code> characters need to be escaped internally, and will be escaped for you).</dd><p class="pad"></p>

<dd>The string matched by <i>STRING</i> is not included in the fields themselves, unless demanded by perl&#39;s regex and split semantics (e.g., regexes in parentheses will add that matched expression as an extra field). See <a href="http://search.cpan.org/perldoc?perlre" class="podlinkpod"
>perlre</a> and <a href="http://search.cpan.org/perldoc?perlfunc#split" class="podlinkpod"
>&#34;split&#34; in perlfunc</a>.</dd><p class="pad"></p>

<dt><a name="R_STRING"
><code lang='und' xml:lang='und'>R</code> <i>STRING</i></a></dt><p class="pad"></p>

<dd>Record separator, defaults to newline.</dd><p class="pad"></p>

<dt><a name="k_pos1[,pos2]"
><code lang='und' xml:lang='und'>k</code> <i>pos1[,pos2]</i></a></dt><p class="pad"></p>

<dd>The keydef argument is a restricted sort key field definition. The format of this definition is:</dd><p class="pad"></p>

<pre lang='und' xml:lang='und'>    field_start[.first_char][type][,field_end[.last_char][type]]</pre>

<dd>where field_start and field_end define a key field restricted to a portion of the line, and type is a modifier from the list of characters <b>b</b>, <b>d</b>, <b>f</b>, <b>i</b>, <b>n</b>, <b>r</b>. The b modifier behaves like the <b>b</b> option, but applies only to the field_start or field_end to which it is attached. The other modifiers behave like the corresponding options, but apply only to the key field to which they are attached; they have this effect if specified with field_start, field_end, or both. If any modifier is attached to a field_start or a field_end, no option applies to either.</dd><p class="pad"></p>

<dd>Occurrences of the <b>k</b> option are significant in command line order. If no <b>k</b> option is specified, a default sort key of the entire line is used. When there are multiple keys fields, later keys are compared only after all earlier keys compare equal.</dd><p class="pad"></p>

<dd>Except when the <b>u</b> option is specified, lines that otherwise compare equal are ordered as if none of the options <b>d</b>, <b>f</b>, <b>i</b>, <b>n</b> or <b>k</b> were present (but with <b>r</b> still in effect, if it was specified) and with all bytes in the lines significant to the comparison. The order in which lines that still compare equal are written is unspecified.</dd><p class="pad"></p>

<dt><a name="+pos1_[-pos2]"
><i>+pos1 [-pos2]</i></a></dt><p class="pad"></p>

<dd>Similar to <b>k</b>, these are mostly obsolete switches, but some people like them and want to use them. Usage is:</dd><p class="pad"></p>

<pre lang='und' xml:lang='und'>    +field_start[.first_char][type] [-field_end[.last_char][type]]</pre>

<dd>Where field_end in <b>k</b> specified the last position to be included, it specifes the last position to NOT be included. Also, numbers are counted from 0 instead of 1. <b>pos2</b> must immediately follow corresponding <b>+pos1</b>. The rest should be the same as the <b>k</b> option.</dd><p class="pad"></p>

<dd>Mixing <b>+pos1</b> <b>pos2</b> with <b>k</b> is allowed, but will result in all of the <b>+pos1</b> <b>pos2</b> options being ordered AFTER the <b>k</b> options. It is best if you Don&#39;t Do That. Pick one and stick with it.</dd><p class="pad"></p>

<dd>Here are some equivalencies:</dd><p class="pad"></p>

<pre lang='und' xml:lang='und'>    +1 -2           =&#62;  -k 2,2
    +1.1 -1.2       =&#62;  -k 2.2,2.2
    +1 -2 +3 -5     =&#62;  -k 2,2 -k 4,5
    +2 +0b -1       =&#62;  -k 3 -k 1b,1
    +2.1 -2.4       =&#62;  -k 3.2,3.4
    +2.0 -3.0       =&#62;  -k 3.1,4.0</pre>
</dl>

<h2><a class='u' href='#___top' title='click to go to top of document'
name="Not_Implemented"
>Not Implemented</a></h2>

<p>If the options are not listed as implemented above, or are not listed in TODO below, they are not in the plan for implementation. This includes <b>T</b> and <b>z</b>.</p>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="EXAMPLES"
>EXAMPLES</a></h1>

<p>Sort file by straight string compare of each line.</p>

<pre lang='und' xml:lang='und'>    sort file</pre>

<p>Sort contents of file by second key in file.</p>

<pre lang='und' xml:lang='und'>    sort -k 2 file</pre>

<p>Sort, in reverse order, contents of file1 and file2, placing output in outfile and using second character of second field as the sort key.</p>

<pre lang='und' xml:lang='und'>    sort -r -k 2.2,2.2 -o outfile file1 file2</pre>

<p>Same sort but sorting numerically on characters 3 through 5 of the fifth field first, and only return records with unique keys.</p>

<pre lang='und' xml:lang='und'>    sort -u -r -k 5.3,5.5rn -k 2.2,2.2 -o outfile file1 file2</pre>

<p>Print passwd(4) file sorted by numeric user ID.</p>

<pre lang='und' xml:lang='und'>    sort -t : -k 3n /etc/passwd</pre>

<p>For the anal sysadmin, check that passwd(4) file is sorted by numeric user ID.</p>

<pre lang='und' xml:lang='und'>    sort -c -t : -k 3n /etc/passwd</pre>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="ENVIRONMENT"
>ENVIRONMENT</a></h1>

<p>Note that if you change the locale settings after the program has started up, you must call setlocale() for the new settings to take effect. For example:</p>

<pre lang='und' xml:lang='und'>    # get constants
    use POSIX &#39;locale_h&#39;;

    # e.g., blank out locale
    $ENV{LC_ALL} = $ENV{LANG} = &#39;&#39;;

    # use new ENV settings
    setlocale(LC_CTYPE, &#39;&#39;);
    setlocale(LC_COLLATE, &#39;&#39;);</pre>

<dl>
<dt><a name="LC_COLLATE"
>LC_COLLATE</a></dt><p class="pad"></p>

<dd>Determine the locale for ordering rules.</dd><p class="pad"></p>

<dt><a name="LC_CTYPE"
>LC_CTYPE</a></dt><p class="pad"></p>

<dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- versus multi-byte characters in arguments and input files) and the behaviour of character classification for the <b>b</b>, <b>d</b>, <b>f</b>, <b>i</b> and <b>n</b> options.</dd><p class="pad"></p>

<dt><a name="MAX_SORT_RECORDS"
>MAX_SORT_RECORDS</a></dt><p class="pad"></p>

<dd>Default is 200,000. Maximum number of records to use before writing to a temp file. Overriden by <b>y</b> option.</dd><p class="pad"></p>

<dt><a name="MAX_SORT_FILES"
>MAX_SORT_FILES</a></dt><p class="pad"></p>

<dd>Maximum number of open temp files to use before merging open temp files. Overriden by <b>F</b> option.</dd><p class="pad"></p>
</dl>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="TODO"
>TODO</a></h1>

<dl>
<dt><a name="Better_debugging_and_error_reporting"
>Better debugging and error reporting</a></dt><p class="pad"></p>

<dt><a name="Performance_hit_with_-u"
>Performance hit with -u</a></dt><p class="pad"></p>

<dt><a name="Do_bytes_instead_of_lines"
>Do bytes instead of lines</a></dt><p class="pad"></p>

<dt><a name="Better_test_suite"
>Better test suite</a></dt><p class="pad"></p>

<dt><a name="Switch_for_turning_off_locale_..._?"
>Switch for turning off locale ... ?</a></dt><p class="pad"></p>
</dl>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="HISTORY"
>HISTORY</a></h1>

<dl>
<dt><a name="v1.00,_Tuesday,_November_13,_2001"
>v1.00, Tuesday, November 13, 2001</a></dt><p class="pad"></p>

<dd>Long overdue release.</dd><p class="pad"></p>

<dd>Add O_TRUNC to output open (D&#39;oh!).</dd><p class="pad"></p>

<dd>Played with somem of the -k options (Marco A. Romero).</dd><p class="pad"></p>

<dd>Fix filehandle close test of STDOUT (Gael Marziou).</dd><p class="pad"></p>

<dd>Some cleanup.</dd><p class="pad"></p>

<dt><a name="v0.68,_Saturday,_February_12,_2000"
>v0.68, Saturday, February 12, 2000</a></dt><p class="pad"></p>

<dd>Closed all files in test.pl so they could be unlinked on some platforms. (Hubert Toullec)</dd><p class="pad"></p>

<dd>Documented <code lang='und' xml:lang='und'>I</code> option. (Hubert Toullec)</dd><p class="pad"></p>

<dd>Removed O_EXCL flag from <code lang='und' xml:lang='und'>sort_file</code>.</dd><p class="pad"></p>

<dd>Fixed bug in sorting multiple files. (Paul Eckert)</dd><p class="pad"></p>

<dt><a name="v0.67_Friday,_April_30,_1999"
>v0.67 Friday, April 30, 1999</a></dt><p class="pad"></p>

<dd>Merged sources back into File::Sort module.</dd><p class="pad"></p>

<dd>Fixed a few bugs, including accepting input via STDIN if no input files given.</dd><p class="pad"></p>

<dt><a name="v0.66_Monday,_April_5,_1999"
>v0.66 Monday, April 5, 1999</a></dt><p class="pad"></p>

<dd>Added <b>+pos1</b> <b>-pos2</b>, and <b>X</b> (regex field separator).</dd><p class="pad"></p>

<dd>More minor fixes to sort subs.</dd><p class="pad"></p>

<dt><a name="v0.65_Sunday,_April_4,_1999"
>v0.65 Sunday, April 4, 1999</a></dt><p class="pad"></p>

<dd>Added <b>b</b> and <b>R</b> (record separator).</dd><p class="pad"></p>

<dd>Made more fixes to sort subs. Seems to be getting pretty stable now, after some serious rewriting. Borrowed several ideas from Albert Dvornik&#39;s implementation.</dd><p class="pad"></p>

<dd>Tidied up docs.</dd><p class="pad"></p>

<dd>Added usage() function.</dd><p class="pad"></p>

<dt><a name="v0.60_Saturday,_April_3,_1999"
>v0.60 Saturday, April 3, 1999</a></dt><p class="pad"></p>

<dd>Rewrote most of the sort sub stuff to be more efficient and in some cases to just make it work right.</dd><p class="pad"></p>

<dd>Can now use multiple <b>k</b> switches. Need Getopt::Long 2.17 (comes with perl 5.005_02). Earlier versions of module work?</dd><p class="pad"></p>

<dd>Still need to go back and implement <b>+pos1</b> <b>-pos2</b> if I feel like it.</dd><p class="pad"></p>

<dt><a name="v0.51_Tuesday,_March_30,_1999"
>v0.51 Tuesday, March 30, 1999</a></dt><p class="pad"></p>

<dd>First round of fixes.</dd><p class="pad"></p>

<dt><a name="v0.50_Monday,_March_29,_1999"
>v0.50 Monday, March 29, 1999</a></dt><p class="pad"></p>

<dd>Rewrote much of the code for the PPT project. Will integrate changes back into the original source, the File::Sort module.</dd><p class="pad"></p>
</dl>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="THANKS"
>THANKS</a></h1>

<p>Mike Blazer &#60;blazer@mail.nevalink.ru&#62;, Vicki Brown &#60;vlb@cfcl.com&#62;, Tom Christiansen &#60;tchrist@perl.com&#62;, Albert Dvornik &#60;bert@mit.edu&#62;, Paul Eckert &#60;peckert@epicrealm.com&#62;, Gene Hsu &#60;gene@moreinfo.com&#62;, Andrew M. Langmead &#60;aml@world.std.com&#62;, Gael Marziou &#60;gael_marziou@hp.com&#62;, Brian L. Matthews &#60;blm@halcyon.com&#62;, Rich Morin &#60;rdm@cfcl.com&#62;, Matthias Neeracher &#60;neeri@iis.ee.ethz.ch&#62;, Miko O&#39;Sullivan &#60;miko@idocs.com&#62;, Tom Phoneix &#60;rootbeer@teleport.com&#62;, Marco A. Romero &#60;mromero@iglou.com&#62;, Gurusamy Sarathy &#60;gsar@activestate.com&#62;, Hubert Toullec &#60;Hubert.Toullec@wanadoo.fr&#62;.</p>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="AUTHOR"
>AUTHOR</a></h1>

<p>Chris Nandor &#60;pudge@pobox.com&#62;, http://pudge.net/</p>

<p>Copyright (c) 1997-2001 Chris Nandor. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License, distributed with Perl.</p>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="VERSION"
>VERSION</a></h1>

<p>v1.00, Tuesday, November 13, 2001</p>

<h1><a class='u' href='#___top' title='click to go to top of document'
name="SEE_ALSO"
>SEE ALSO</a></h1>

<p>PPT project, &#60;URL:http://sf.net/projects/ppt/&#62;, sort(1), File::Sort, locale.</p>

<!-- end doc -->

</body></html>