jcs's openbsd hax
openbsd
at jcs 628 lines 17 kB view raw
1.\" $OpenBSD: sort.1,v 1.69 2025/04/01 00:18:28 schwarze Exp $ 2.\" 3.\" Copyright (c) 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" the Institute of Electrical and Electronics Engineers, Inc. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. Neither the name of the University nor the names of its contributors 18.\" may be used to endorse or promote products derived from this software 19.\" without specific prior written permission. 20.\" 21.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31.\" SUCH DAMAGE. 32.\" 33.\" @(#)sort.1 8.1 (Berkeley) 6/6/93 34.\" 35.Dd $Mdocdate: April 1 2025 $ 36.Dt SORT 1 37.Os 38.Sh NAME 39.Nm sort 40.Nd sort, merge, or sequence check text and binary files 41.Sh SYNOPSIS 42.Nm sort 43.Op Fl bCcdfgHhiMmnRrsuVz 44.Op Fl k Ar field1 Ns Op , Ns Ar field2 45.Op Fl o Ar output 46.Op Fl S Ar size 47.Op Fl T Ar dir 48.Op Fl t Ar char 49.Op Ar 50.Sh DESCRIPTION 51The 52.Nm 53utility sorts the lines of text or binary files. 54A line is a record separated from the subsequent record by a 55newline (default) or NUL 56.Ql \e0 57character 58.Po 59.Fl z 60option 61.Pc . 62A record can contain any printable or unprintable characters. 63Comparisons are based on one or more sort keys extracted from 64each line according to the specified command line options. 65By default, 66.Nm 67uses entire lines for comparison and sorts in 68.Xr ascii 7 69order. 70.Pp 71If no 72.Ar file 73is specified, or if 74.Ar file 75is 76.Sq - , 77the standard input is used. 78.Pp 79The options are as follows: 80.Bl -tag -width Ds 81.It Fl C , Fl Fl check Ns = Ns Cm silent Ns | Ns Cm quiet 82Check that the single input file is sorted. 83If it is, exit 0; if it's not, exit 1. 84In either case, produce no output. 85.It Fl c , Fl Fl check 86Like 87.Fl C , 88but additionally write a message to 89.Em stderr 90if the input file is not sorted. 91.It Fl m , Fl Fl merge 92Merge only; the input files are assumed to be pre-sorted. 93If they are not sorted, the output order is undefined. 94.It Fl o Ar output , Fl Fl output Ns = Ns Ar output 95Write the output to the 96.Ar output 97file instead of the standard output. 98This file can be the same as one of the input files. 99.It Fl S Ar size , Fl Fl buffer-size Ns = Ns Ar size 100Use a memory buffer no larger than 101.Ar size . 102The modifiers %, b, K, M, G, T, P, E, Z, and Y can be used. 103If no memory limit is specified, 104.Nm 105may use up to about 90% of available memory. 106If the input is too big to fit into the memory buffer, 107temporary files are used. 108.It Fl s 109Stable sort; maintains the original record order of records that have 110an equal key. 111This is a non-standard feature, but it is widely accepted and used. 112.It Fl T Ar dir , Fl Fl temporary-directory Ns = Ns Ar dir 113Store temporary files in the directory 114.Ar dir . 115The default path is the value of the environment variable 116.Ev TMPDIR 117or 118.Pa /tmp 119if 120.Ev TMPDIR 121is not defined. 122.It Fl u , Fl Fl unique 123Unique: suppress all but one in each set of lines having equal keys. 124This option implies 125.Fl s . 126If used with 127.Fl C 128or 129.Fl c , 130.Nm 131also checks that there are no lines with duplicate keys. 132.El 133.Pp 134The following options override the default ordering rules. 135If ordering options appear before the first 136.Fl k 137option, they apply globally to all sort keys. 138When attached to a specific key (see 139.Fl k ) , 140the ordering options override all global ordering options for that key. 141Note that the ordering options intended to apply globally should not 142appear after 143.Fl k 144or results may be unexpected. 145.Bl -tag -width indent 146.It Fl d , Fl Fl dictionary-order 147Consider only blank spaces and alphanumeric characters in comparisons. 148.It Fl f , Fl Fl ignore-case 149Consider all lowercase characters that have uppercase 150equivalents to be the same for purposes of comparison. 151.It Fl g , Fl Fl general-numeric-sort , Fl Fl sort Ns = Ns Cm general-numeric 152Use an initial numeric string as the key and sort numerically. 153As opposed to 154.Fl n , 155this option handles general floating point numbers. 156It has a more 157permissive format than that allowed by 158.Fl n 159but it has a significant performance drawback. 160.It Fl h , Fl Fl human-numeric-sort , Fl Fl sort Ns = Ns Cm human-numeric 161Use an initial numeric string with an optional SI suffix as the key. 162Sorts first by numeric sign (negative, zero, or 163positive); then by SI suffix (either empty, or `k' or `K', or one 164of `MGTPEZY', in that order); and finally by numeric value. 165The SI suffix must immediately follow the number. 166For example, '12345K' sorts before '1M', because M is "larger" than K. 167This sort option is useful for sorting the output of a single invocation 168of a 169.Xr df 1 170command with 171.Fl h 172or 173.Fl H 174options (human-readable). 175.It Fl i , Fl Fl ignore-nonprinting 176Ignore all non-printable characters. 177.It Fl M , Fl Fl month-sort , Fl Fl sort Ns = Ns Cm month 178Sort by month abbreviations. 179Unknown strings are considered smaller than valid month names. 180.It Fl n , Fl Fl numeric-sort , Fl Fl sort Ns = Ns Cm numeric 181Use an initial numeric string as the key, consisting of optional 182blank space, an optional minus sign, and zero or more digits including 183an optional decimal point, and sort numerically. 184Leading blank characters are ignored. 185.It Fl R , Fl Fl random-sort , Fl Fl sort Ns = Ns Cm random 186Sort lines in random order. 187This is a random permutation of the inputs with the exception that 188equal keys sort together. 189It is implemented by hashing the input keys and sorting the hash values. 190The hash function is randomized with data from 191.Xr arc4random_buf 3 , 192or by file content if one is specified via 193.Fl Fl random-source . 194If multiple sort fields are specified, 195the same random hash function is used for all of them. 196.It Fl r , Fl Fl reverse 197Sort in reverse order. 198.It Fl V , Fl Fl version-sort 199This option is intended to sort strings that contain version numbers 200but it can be used for other purposes as well, for example to sort 201IPv4 addresses in dotted quad notation. 202.Pp 203When comparing two strings, both strings are split into substrings 204such that the first and every other odd-numbered substring 205consists of non-digit characters only, 206while every even-numbered substring consists of digits only. 207These substrings are compared in turn from left to right 208until a difference is found. 209The first substring can be empty; all others cannot. 210.Pp 211Non-digit substrings are compared alphabetically, with upper case 212letters sorting before lower case letters, letters sorting before 213non-letters, and non-letters sorting in 214.Xr ascii 7 215order. 216Substrings consisting of digits are compared as integer numbers. 217.Pp 218At the end of each string, zero or more suffixes that start with a dot, 219consist only of letters, digits, and tilde characters, and do not 220start with a digit are ignored, equivalent to the regular expression 221"(\e.([A-Za-z~][A-Za-z0-9~]*)?)*". 222This is intended for ignoring filename suffixes such as 223.Dq .tar.bz2 . 224.Pp 225In the following example, the first substring is 226.Qq sort\- 227and the other odd-numbered substrings are all 228.Qq \&. : 229.Bd -literal -offset indent 230$ ls sort* | sort -V 231sort-1.022.tgz 232sort-1.23.tgz 233sort-1.23.1.tgz 234sort-1.024.tgz 235sort-1.024.003. 236sort-1.024.003.tgz 237sort-1.024.07.tgz 238sort-1.024.009.tgz 239.Ed 240.El 241.Pp 242The treatment of field separators can be altered using these options: 243.Bl -tag -width indent 244.It Fl b , Fl Fl ignore-leading-blanks 245Ignore leading blank space when determining the start 246and end of a restricted sort key (see 247.Fl k ) . 248If 249.Fl b 250is specified before the first 251.Fl k 252option, it applies globally to all key specifications. 253Otherwise, 254.Fl b 255can be attached independently to each 256.Ar field 257argument of the key specifications. 258Note that 259.Fl b 260should not appear after 261.Fl k , 262and that it has no effect unless key fields are specified. 263.It Xo 264.Fl k Ar field1 Ns Op , Ns Ar field2 , 265.Fl Fl key Ns = Ns Ar field1 Ns Op , Ns Ar field2 266.Xc 267Define a restricted sort key that has the starting position 268.Ar field1 , 269and optional ending position 270.Ar field2 271of a key field. 272The 273.Fl k 274option may be specified multiple times, 275in which case subsequent keys are compared after earlier keys compare equal. 276The 277.Fl k 278option replaces the obsolete options 279.Cm \(pl Ns Ar pos1 280and 281.Fl Ns Ar pos2 , 282but the old notation is also supported. 283.It Fl t Ar char , Fl Fl field-separator Ns = Ns Ar char 284Use 285.Ar char 286as the field separator character. 287The initial 288.Ar char 289is not considered to be part of a field when determining key offsets. 290Each occurrence of 291.Ar char 292is significant (for example, 293.Dq Ar charchar 294delimits an empty field). 295If 296.Fl t 297is not specified, the default field separator is a sequence of 298blank-space characters, and consecutive blank spaces do 299.Em not 300delimit an empty field; further, the initial blank space 301.Em is 302considered part of a field when determining key offsets. 303To use NUL as field separator, use 304.Fl t 305\(aq\e0\(aq. 306.It Fl z , Fl Fl zero-terminated 307Use NUL as the record separator. 308By default, records in the files are expected to be separated by 309the newline characters. 310With this option, NUL 311.Pq Ql \e0 312is used as the record separator character. 313.El 314.Pp 315Other options: 316.Bl -tag -width indent 317.It Fl Fl batch-size Ns = Ns Ar num 318Specify maximum number of files that can be opened by 319.Nm 320at once. 321This option affects behavior when having many input files or using 322temporary files. 323The minimum value is 2. 324The default value is 16. 325.It Fl Fl compress-program Ns = Ns Ar program 326Use 327.Ar program 328to compress temporary files. 329When invoked with no arguments, 330.Ar program 331must compress standard input to standard output. 332When called with the 333.Fl d 334option, it must decompress standard input to standard output. 335If 336.Ar program 337fails, 338.Nm 339will exit with an error. 340The 341.Xr compress 1 342and 343.Xr gzip 1 344utilities meet these requirements. 345.It Fl Fl debug 346Print some extra information about the sorting process to the 347standard output. 348.It Fl Fl files0-from Ns = Ns Ar filename 349Take the input file list from the file 350.Ar filename . 351The file names must be separated by NUL 352(like the output produced by the command 353.Dq find ... -print0 ) . 354.It Fl Fl heapsort 355Try to use heap sort, if the sort specifications allow. 356This sort algorithm cannot be used with 357.Fl u 358and 359.Fl s . 360.It Fl Fl help 361Print the help text and exit. 362.It Fl H , Fl Fl mergesort 363Use mergesort. 364This is a universal algorithm that can always be used, 365but it is not always the fastest. 366.It Fl Fl mmap 367Try to use file memory mapping system call. 368It may increase speed in some cases. 369.It Fl Fl qsort 370Try to use quick sort, if the sort specifications allow. 371This sort algorithm cannot be used with 372.Fl u 373and 374.Fl s . 375.It Fl Fl radixsort 376Try to use radix sort, if the sort specifications allow. 377The radix sort can only be used for trivial locales (C and POSIX), 378and it cannot be used for numeric or month sort. 379Radix sort is very fast and stable. 380.It Fl Fl random-source Ns = Ns Ar filename 381For random sort, the contents of 382.Ar filename 383are used as the source of the 384.Sq seed 385data for the hash function. 386Two invocations of random sort with the same seed data 387produce the same result if the input is also identical. 388By default, the 389.Xr arc4random_buf 3 390function is used instead. 391.It Fl Fl version 392Print the version and exit. 393.El 394.Pp 395A field is defined as a maximal sequence of characters other than the 396field separator and record separator 397.Pq newline by default . 398Initial blank spaces are included in the field unless 399.Fl b 400has been specified; 401the first blank space of a sequence of blank spaces acts as the field 402separator and is included in the field (unless 403.Fl t 404is specified). 405For example, by default all blank spaces at the beginning of a line are 406considered to be part of the first field. 407.Pp 408Fields are specified by the 409.Fl k Ar field1 Ns Op , Ns Ar field2 410option. 411If 412.Ar field2 413is missing, the end of the key defaults to the end of the line. 414.Pp 415The arguments 416.Ar field1 417and 418.Ar field2 419have the form 420.Em m.n 421.Em (m,n > 0) 422and can be followed by one or more of the modifiers 423.Cm b , d , f , i , 424.Cm n , g , M 425and 426.Cm r , 427which correspond to the options discussed above. 428When 429.Cm b 430is specified, it applies only to 431.Ar field1 432or 433.Ar field2 434where it is specified while the rest of the modifiers 435apply to the whole key field regardless if they are 436specified only with 437.Ar field1 438or 439.Ar field2 440or both. 441A 442.Ar field1 443position specified by 444.Em m.n 445is interpreted as the 446.Em n Ns th 447character from the beginning of the 448.Em m Ns th 449field. 450A missing 451.Em \&.n 452in 453.Ar field1 454means 455.Ql \&.1 , 456indicating the first character of the 457.Em m Ns th 458field; if the 459.Fl b 460option is in effect, 461.Em n 462is counted from the first non-blank character in the 463.Em m Ns th 464field; 465.Em m Ns \&.1b 466refers to the first non-blank character in the 467.Em m Ns th 468field. 469.No 1\&. Ns Em n 470refers to the 471.Em n Ns th 472character from the beginning of the line; 473if 474.Em n 475is greater than the length of the line, the field is taken to be empty. 476.Pp 477.Em n Ns th 478positions are always counted from the field beginning, even if the field 479is shorter than the number of specified positions. 480Thus, the key can really start from a position in a subsequent field. 481.Pp 482A 483.Ar field2 484position specified by 485.Em m.n 486is interpreted as the 487.Em n Ns th 488character (including separators) from the beginning of the 489.Em m Ns th 490field. 491A missing 492.Em \&.n 493indicates the last character of the 494.Em m Ns th 495field; 496.Em m 497= \&0 498designates the end of a line. 499Thus the option 500.Fl k Ar v.x,w.y 501is synonymous with the obsolete option 502.Cm \(pl Ns Ar v-\&1.x-\&1 503.Fl Ns Ar w-\&1.y ; 504when 505.Em y 506is omitted, 507.Fl k Ar v.x,w 508is synonymous with 509.Cm \(pl Ns Ar v-\&1.x-\&1 510.Fl Ns Ar w\&.0 . 511The obsolete 512.Cm \(pl Ns Ar pos1 513.Fl Ns Ar pos2 514option is still supported, except for 515.Fl Ns Ar w\&.0b , 516which has no 517.Fl k 518equivalent. 519.Sh ENVIRONMENT 520.Bl -tag -width Ds 521.It Ev TMPDIR 522Path to the directory in which temporary files will be stored. 523Note that 524.Ev TMPDIR 525may be overridden by the 526.Fl T 527option. 528.El 529.Sh FILES 530.Bl -tag -width Pa -compact 531.It Pa /tmp/.bsdsort.PID.* 532Temporary files. 533.El 534.Sh EXIT STATUS 535The 536.Nm 537utility exits with one of the following values: 538.Pp 539.Bl -tag -width Ds -offset indent -compact 540.It 0 541Successfully sorted the input files or if used with 542.Fl C 543or 544.Fl c , 545the input file already met the sorting criteria. 546.It 1 547On disorder (or non-uniqueness) with the 548.Fl C 549or 550.Fl c 551options. 552.It 2 553An error occurred. 554.El 555.Sh SEE ALSO 556.Xr comm 1 , 557.Xr join 1 , 558.Xr uniq 1 559.Sh STANDARDS 560The 561.Nm 562utility is compliant with the 563.St -p1003.1-2008 564specification, except that it ignores the user's 565.Xr locale 1 566and always assumes 567.Ev LC_ALL Ns =C. 568.Pp 569The flags 570.Op Fl gHhiMRSsTVz 571are extensions to that specification. 572.Pp 573All long options are extensions to the specification. 574Some are provided for compatibility with GNU 575.Nm , 576others are specific to this implementation. 577.Pp 578Some implementations of 579.Nm 580honor the 581.Fl b 582option even when no key fields are specified. 583This implementation follows historic practice and 584.St -p1003.1-2008 585in only honoring 586.Fl b 587when it precedes a key field. 588.Pp 589The historic practice of allowing the 590.Fl o 591option to appear after the 592.Ar file 593is supported for compatibility with older versions of 594.Nm . 595.Pp 596The historic key notations 597.Cm \(pl Ns Ar pos1 598and 599.Fl Ns Ar pos2 600are supported for compatibility with older versions of 601.Nm 602but their use is highly discouraged. 603.Sh HISTORY 604A 605.Nm 606command appeared in 607.At v1 . 608.Sh AUTHORS 609.An Gabor Kovesdan Aq Mt gabor@FreeBSD.org 610.An Oleg Moskalenko Aq Mt mom040267@gmail.com 611.Sh CAVEATS 612This implementation of 613.Nm 614has no limits on input line length (other than imposed by available 615memory) or any restrictions on bytes allowed within lines. 616.Pp 617The performance depends highly on 618efficient choice of sort keys and key complexity. 619The fastest sort is on whole lines, with option 620.Fl s . 621For the key specification, the simpler to process the 622lines the faster the search will be. 623.Pp 624When sorting by arithmetic value, using 625.Fl n 626results in much better performance than 627.Fl g 628so its use is encouraged whenever possible.