SORT(1)
π 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:
IINPUT β 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? πoOUTPUT β 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 thecoption, check that there are no lines with consecutive lines with duplicate keys, in addition to checking that the input file is sorted. β¨yMAX_SORT_RECORDS β maximum number of lines (records) read before writing to a temp file. default is200,000. this may eventually change to be kbytes instead of lines. lines was easier to implement. can also specify with theMAX_SORT_RECORDSenvironment variable. we love a configurable queen πFMAX_SORT_FILES β maximum number of temp files to be held open at once. default to40, as older Windows ports had quite a small limit. can also specify with theMAX_SORT_FILESenvironment variable. no temp files will be used at all ifMAX_SORT_RECORDSis 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.doverridesi. 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 theboption is specified before the firstkoption, it is applied to allkoptions. otherwise, theboption can be attached independently to eachfield_startorfield_endoption argument (see below). bestie, pay attention to the order.tSTRING β use STRING as the field separator character;charis not considered to be part of a field (although it can be included in a sort key). each occurrence ofcharis significant (for example,<char>""<char>delimits an empty field). iftis 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 πXSTRING β same ast, 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). seeperlreand"split" in perlfunc. regex girlies rise πRSTRING β record separator, defaults to newline. literally what it says.kpos1[,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 tok, 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 theb,d,f,iandnoptions. characters are complicated, okay?MAX_SORT_RECORDSβ default is200,000. maximum number of records to use before writing to a temp file. overridden byyoption. theyoption said "i'm the main character."MAX_SORT_FILESβ maximum number of open temp files to use before merging open temp files. overridden byFoption. 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_TRUNCto output open (D'oh!). - Played with somem of the
-koptions (Marco A. Romero). - Fix filehandle close test of STDOUT (Gael Marziou).
- Some cleanup.
v0.68, Saturday, February 12, 2000
- Closed all files in
test.plso they could be unlinked on some platforms. (Hubert Toullec) - Documented
"I"option. (Hubert Toullec) - Removed
O_EXCLflag from"sort_file". - Fixed bug in sorting multiple files. (Paul Eckert)
v0.67 Friday, April 30, 1999
- Merged sources back into
File::Sortmodule. - Fixed a few bugs, including accepting input via STDIN if no input files given.
v0.66 Monday, April 5, 1999
- Added
+pos1 -pos2, andX(regex field separator). - More minor fixes to sort subs.
v0.65 Sunday, April 4, 1999
- Added
bandR(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
kswitches. NeedGetopt::Long 2.17(comes with perl5.005_02). Earlier versions of module work? - Still need to go back and implement
+pos1 -pos2if 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::Sortmodule.
π 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:
- PPT project,
<URL:http://sf.net/projects/ppt/> sort(1)File::Sortlocale
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
- alpine 3.24: perl-power-tools-doc 1.053-r1 Β· GPL-2.0-or-later AND Artistic-1.0-Perl AND Artistic-2.0 AND BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms