SORT(1)

πŸ“¦ version 1.053, in alpine 3.24

πŸ’– sort(1) β€” the manual, but make it cute πŸ’–

okay so this is sort(1) from the User Contributed Perl Documentation, and I'm gonna walk you through it like we're in the group chat. it's literally a whole vibe: sorting and merging text files, but make it perl. no cap, bestie, let's get into it 🫢


✨ NAME ✨

sort β€” sort or merge text files. that's it. that's the whole gig. iconic behavior honestly πŸ’…


🌸 SYNOPSIS 🌸

  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] [file ...]

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so the sort utility sorts text files by lines (or records, if you're fancy like that). comparisons are based on one or more sort keys extracted from each line of input, and they're performed lexicographically. by default, if keys are not given, sort regards each input line as a single field. the sort is a merge sort. and if you don't provide any file arguments, standard input is used. periodt πŸ’«


πŸŽ€ OPTIONS πŸŽ€

the following options are available, bestie. every single one. no skipping:

  • I INPUT β€” pass in the input file(s). this can be either a single string with the filename, or an array reference containing multiple filename strings. lowkey flexible πŸ’…
  • c β€” check that a single input file 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. it's giving "just tell me if it's sorted" energy.
  • m β€” merge only; the input files are assumed to already be sorted. don't come for me if they're not, okay? 😭
  • o OUTPUT β€” specify the name of an OUTPUT file to be used instead of the standard output. because sometimes you want the tea saved to a file.
  • u β€” unique: suppresses all but one in each set of lines having equal keys. if used with the c option, check that there are no lines with consecutive lines with duplicate keys, in addition to checking that the input file is sorted. ✨
  • y MAX_SORT_RECORDS β€” maximum number of lines (records) read before writing to a 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 the MAX_SORT_RECORDS environment variable. we love a configurable queen πŸ‘‘
  • F MAX_SORT_FILES β€” 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 the MAX_SORT_FILES environment variable. no temp files will be used at all if MAX_SORT_RECORDS is never reached. ngl that's kinda neat.
  • D β€” send debugging information to STDERR. behavior subject to change. it's giving chaotic but useful πŸ’€

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 k), the specified ordering options override all global ordering options for that key. slay.

  • d β€” specify that only blank characters and alphanumeric characters, according to the current locale setting, are significant in comparisons. d overrides i. noted, bestie.
  • f β€” 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. case-insensitive-ish, but make it locale.
  • i β€” ignores all characters that are non-printable, according to the current locale setting. bye bye invisible characters ✨
  • n β€” does numeric instead of string compare, using whatever perl considers to be a number in numeric comparisons. numbers > vibes.
  • r β€” reverse the sense of the comparisons. plot twist πŸ”₯
  • b β€” ignore leading blank characters when determining the starting and ending positions of a restricted sort key. if the b option is specified before the first k option, it is applied to all k options. otherwise, the b option can be attached independently to each field_start or field_end option argument (see below). bestie, pay attention to the order.
  • t STRING β€” use STRING 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, <char>""<char> delimits an empty field). if t 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. okay so it's giving delimiter energy πŸ’…
  • X STRING β€” same as t, but STRING is interpreted as a Perl regular expression instead. do not escape any characters (/ characters need to be escaped internally, and will be escaped for you). the string matched by STRING is not included in the fields themselves, unless demanded by perl's regex and split semantics (e.g., regexes in parentheses will add that matched expression as an extra field). see perlre and "split" in perlfunc. regex girlies rise πŸ™ˆ
  • R STRING β€” record separator, defaults to newline. literally what it says.
  • k pos1[,pos2] β€” the keydef argument is a restricted sort key field definition. the format of this definition is:
      field_start[.first_char][type][,field_end[.last_char][type]]

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, d, f, i, n, r. the b modifier behaves like the 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. πŸ’€

occurrences of the k option are significant in command line order. if no k 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. bestie, it's layered.

except when the u option is specified, lines that otherwise compare equal are ordered as if none of the options d, f, i, n or k were present (but with r 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. so don't @ me about tie-breaking. 😭

  • +pos1 [-pos2] β€” similar to k, these are mostly obsolete switches, but some people like them and want to use them. usage is:
      +field_start[.first_char][type] [-field_end[.last_char][type]]

where field_end in k specified the last position to be included, it specifies the last position to NOT be included. also, numbers are counted from 0 instead of 1. pos2 must immediately follow corresponding +pos1. the rest should be the same as the k option. girlies who love legacy flags, this one's for you πŸŽ€

mixing +pos1 pos2 with k is allowed, but will result in all of the +pos1 pos2 options being ordered AFTER the k options. it is best if you Don't Do That. pick one and stick with it. no cap, just pick a lane.

here are some equivalencies:

      +1 -2           =>  -k 2,2
      +1.1 -1.2       =>  -k 2.2,2.2
      +1 -2 +3 -5     =>  -k 2,2 -k 4,5
      +2 +0b -1       =>  -k 3 -k 1b,1
      +2.1 -2.4       =>  -k 3.2,3.4
      +2.0 -3.0       =>  -k 3.1,4.0

🚫 Not Implemented 🚫

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 T and z. so don't ask. it's not happening. periodt πŸ’…


⭐ EXAMPLES ⭐

sort file by straight string compare of each line. simple. clean. iconic.

    sort file

sort contents of file by second key in file. you're welcome.

    sort -k 2 file

sort, in reverse order, contents of file1 and file2, placing output in outfile and using second character of second field as the sort key. look at you go πŸ¦‹

    sort -r -k 2.2,2.2 -o outfile file1 file2

same sort but sorting numerically on characters 3 through 5 of the fifth field first, and only return records with unique keys. highkey a flex.

    sort -u -r -k 5.3,5.5rn -k 2.2,2.2 -o outfile file1 file2

print passwd(4) file sorted by numeric user ID. sysadmin bestie vibes.

    sort -t : -k 3n /etc/passwd

for the anal sysadmin, check that passwd(4) file is sorted by numeric user ID. we love a verification moment ✨

    sort -c -t : -k 3n /etc/passwd

🌈 ENVIRONMENT 🌈

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:

    # get constants
    use POSIX 'locale_h';

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

    # use new ENV settings
    setlocale(LC_CTYPE, '');
    setlocale(LC_COLLATE, '');
  • LC_COLLATE β€” determine the locale for ordering rules. this is the sorting vibe check.
  • LC_CTYPE β€” 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, d, f, i and n options. characters are complicated, okay?
  • MAX_SORT_RECORDS β€” default is 200,000. maximum number of records to use before writing to a temp file. overridden by y option. the y option said "i'm the main character."
  • MAX_SORT_FILES β€” maximum number of open temp files to use before merging open temp files. overridden by F option. again, the option wins. πŸ‘‘

🧚 TODO 🧚

the vibe to-do list, straight from the source:

  • Better debugging and error reporting
  • Performance hit with -u
  • Do bytes instead of lines
  • Better test suite
  • Switch for turning off locale ... ?

it's giving "we know, we know, it's on the list." 😭


πŸ’œ HISTORY πŸ’œ

v1.00, Tuesday, November 13, 2001

  • Long overdue release.
  • Add O_TRUNC to output open (D'oh!).
  • Played with somem of the -k options (Marco A. Romero).
  • Fix filehandle close test of STDOUT (Gael Marziou).
  • Some cleanup.

v0.68, Saturday, February 12, 2000

  • Closed all files in test.pl so they could be unlinked on some platforms. (Hubert Toullec)
  • Documented "I" option. (Hubert Toullec)
  • Removed O_EXCL flag from "sort_file".
  • Fixed bug in sorting multiple files. (Paul Eckert)

v0.67 Friday, April 30, 1999

  • Merged sources back into File::Sort module.
  • Fixed a few bugs, including accepting input via STDIN if no input files given.

v0.66 Monday, April 5, 1999

  • Added +pos1 -pos2, and X (regex field separator).
  • More minor fixes to sort subs.

v0.65 Sunday, April 4, 1999

  • Added b and R (record separator).
  • Made more fixes to sort subs. Seems to be getting pretty stable now, after some serious rewriting. Borrowed several ideas from Albert Dvornik's implementation.
  • Tidied up docs.
  • Added usage() function.

v0.60 Saturday, April 3, 1999

  • Rewrote most of the sort sub stuff to be more efficient and in some cases to just make it work right.
  • Can now use multiple k switches. Need Getopt::Long 2.17 (comes with perl 5.005_02). Earlier versions of module work?
  • Still need to go back and implement +pos1 -pos2 if I feel like it.

v0.51 Tuesday, March 30, 1999

  • First round of fixes.

v0.50 Monday, March 29, 1999

  • Rewrote much of the code for the PPT project. Will integrate changes back into the original source, the File::Sort module.

πŸ’– THANKS πŸ’–

Okay so this page literally would not exist without a whole squad of absolute legends, and we are not about to do them dirty by not naming every single one. So here's the full roll call, no cap, every name and every address, exactly as it should be:

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

Like, sixteen whole humans contributed to this. Sixteen! We stan a collaborative effort πŸ’…


πŸ‘‘ AUTHOR πŸ‘‘

The one and only main character behind all this:

Chris Nandor "<pudge@pobox.com>", http://pudge.net/

And the legal bit, because bestie we have to include it and it's lowkey important:

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.

Translation: it's free, it's open, you can share it and remix it β€” but under the Artistic License that ships with Perl, and the copyright notice stays. Respect the paperwork, it's giving legally iconic πŸŽ€

πŸ¦‹ VERSION πŸ¦‹

v1.00, Tuesday, November 13, 2001

Yes, the date is that date. It's vintage. It's a classic. It's giving "we've been doing this since before you could read" ✨


🌈 SEE ALSO 🌈

If you're the kind of person who finishes one manpage and immediately wants more (iconic behaviour, honestly), go touch grass in these:


And that's the whole page, bestie β€” you made it to the end 🫢 Go forth and sort things responsibly.

πŸ’œ Sign-off time: this gorgeous little manpage is from perl v5.42.2, dated 2026-02-15, and it's filed under SORT(1) because that's its whole identity. Periodt. πŸ’…

πŸ“¦ where this page comes from