Documentation
¶
Overview ¶
Package sort implements the sort builtin command.
sort — sort lines of text files
Usage: sort [OPTION]... [FILE]...
Write sorted concatenation of all FILE(s) to standard output. With no FILE, or when FILE is -, read standard input.
Accepted flags:
-r, --reverse
Reverse the result of comparisons (sort descending).
-n, --numeric-sort
Compare according to string numerical value.
-u, --unique
Output only the first of an equal run.
-k, --key=KEYDEF
Sort via a key definition; KEYDEF is F[.C][OPTS][,F[.C][OPTS]].
-t, --field-separator=SEP
Use SEP as the field separator.
-b, --ignore-leading-blanks
Ignore leading blanks when finding sort keys.
-f, --ignore-case
Fold lowercase to uppercase for comparisons.
-d, --dictionary-order
Consider only blanks and alphanumeric characters.
-c
Check whether input is sorted; exit 1 if not.
-C, --check=silent
Like -c but do not print the diagnostic line.
-s, --stable
Stabilize sort by disabling last-resort comparison.
-h, --help
Print usage to stdout and exit 0.
Rejected flags (unsafe):
-o FILE (writes to filesystem) -T DIR (writes temp files) --compress-program (executes a binary)
Exit codes:
0 Success (or input is sorted when using -c/-C). 1 Error, or input is NOT sorted when using -c/-C.
Memory safety:
sort must buffer all input before producing output. A maximum of MaxLines (1,000,000) lines is enforced to prevent OOM. Per-line cap of MaxLineBytes (1 MiB) is enforced via the scanner. All loops check ctx.Err() at each iteration to honour the shell's execution timeout.
Index ¶
Constants ¶
const MaxLineBytes = 1 << 20 // 1 MiB
MaxLineBytes is the per-line buffer cap for the line scanner.
const MaxLines = 1_000_000
MaxLines is the maximum number of lines sort will buffer. Beyond this the command errors out to prevent unbounded memory growth.
const MaxTotalBytes = 5 * 1024 * 1024 // 5 MiB
MaxTotalBytes is the cumulative byte cap across all input lines. This prevents OOM when many lines are each below MaxLineBytes but collectively consume excessive memory — especially in sort chains where N concurrent sort instances each hold their full input buffer simultaneously.
Variables ¶
var Cmd = builtins.Command{
Name: "sort",
Description: "sort lines of text files",
MakeFlags: registerFlags,
}
Cmd is the sort builtin command descriptor.
Functions ¶
This section is empty.
Types ¶
This section is empty.