← All 153 books sort, one decision per page
One decision per page

sort, one decision per page

Twenty-eight lessons on the sort command, one sorting decision per page: the line sorter that takes the whole line as its key by default; the text trap that puts 10 before 9 and the -n that fixes it; -r to reverse; fields and keys with -k, key ranges that end where they should, -t separators and two-key tie-breaks; -u for one of each; -M for months and -h for human sizes like 2K and 1G; stability and -s; the -c and -C checks before you trust a file; the locale surprise and LC_ALL=C; -b for leading blanks; uniq next door with its adjacency rule, uniq -c counting and the sort, count, sort ladder; -m merge, -o output and the dash for standard input; mergesort under the hood; the kin cut and comm on sorted files; and teaching a friend one honest hour.


Steve Hodgkiss 7 lessons

One decision, one page, one honest hour. That's the rhythm.

sort, one decision per page

Twenty-eight lessons on the sort command, one sorting decision per page: the line sorter that takes the whole line as its key by default; the text trap that puts 10 before 9 and the -n that fixes it; -r to reverse; fields and keys with -k, key ranges that end where they should, -t separators and two-key tie-breaks; -u for one of each; -M for months and -h for human sizes like 2K and 1G; stability and -s; the -c and -C checks before you trust a file; the locale surprise and LC_ALL=C; -b for leading blanks; uniq next door with its adjacency rule, uniq -c counting and the sort, count, sort ladder; -m merge, -o output and the dash for standard input; mergesort under the hood; the kin cut and comm on sorted files; and teaching a friend one honest hour.


Set in Space Grotesk, Inter and JetBrains Mono (SIL Open Font License).

Every fact in this book is as the official source states it, fetched and read during this build: the local manual pages sort(1), uniq(1), cut(1) and comm(1) (GNU coreutils 9.4), the definition of truth for this book, and the Wikipedia articles Sort (Unix), uniq, Comm, Cut (Unix), Sorting algorithm and POSIX. No facts are sourced from Reddit, Stack Overflow, vendor blogs or commercial sites. Every command and output pair was run on this machine with temporary files under the build scratch directory only, each capture cached verbatim so the pair is reproducible. SAFETY LAW: no file outside the book directory's scratch area was written by any demonstration; the -o option wrote only to a scratch copy, and the check flags -c and -C never wrote anything. Honest notes: the Wikipedia titles Uniq (Unix) and Comm (Unix) are redirects (to uniq and Comm), so both were followed and read at their redirect goals. An independent guide, not affiliated with or endorsed by the Free Software Foundation, the IEEE, or the Wikimedia Foundation.

Your purchase is for personal use only. You do not have redistribution rights: please do not share, resell, or republish this book or its pages.

© 2026 Steve Hodgkiss. All rights reserved. Personal use only; no redistribution rights.

Edition 1.0 · stevehodgkiss.net

Contents

Contents


Part 1 · The plain sort
Reverse with -r4
Part 2 · Keys and fields
The key range5
Part 3 · Other orders
Stable order6
Part 4 · Trust but check
Blanks ignored7
Part 5 · uniq in the line
The count ladder8
Part 6 · Merge and output
The -o output9
Part 7 · The kin
Teaching sort10

Per the Wikipedia Sort (Unix) article, the -r flag will reverse the sort order. Per the sort(1) manual page, -r, --reverse reverses the result of comparisons. This machine's capture: sort -r numbers.txt printed 9, 2, 10, 1, and sort -rn numbers.txt printed 10, 9, 2, 1.

Sorting tools companion · No. 01
the flip

Reverse with -r

the same order, backwards

the same order, walked backwards$ sort -rn numbers.txt12910ascending, the -n order-r flips it10921descending, our capturetwo decisions stacked:numeric first, reverse second-r reverses the result of comparisons and nothing else

-r is the cleanest decision in the book: per the sort(1) manual page it reverses the result of comparisons, nothing else. The Wikipedia article says the same: the -r flag will reverse the sort order.

Our capture: sort -r numbers.txt printed 9, 2, 10, 1, the text order walked backwards. Add -n and the flip lands on the numeric order: sort -rn printed 10, 9, 2, 1.

Read -rn as two decisions stacked: numeric first, reverse second.

ONE HONEST HOUR

Sort your number file with sort -rn and with sort -r, and write down which flag pair gave 10 first and why.

Per the Wikipedia Sort (Unix) article, -k2,2n specifies sorting on the key starting and ending with column 2, and sorting numerically, and if -k2 is used instead, the sort key would begin at column 2 and extend to the end of the line, spanning all the fields in between. This machine's capture: on pets.txt holding zed bird aaa and amy bird zzz, sort -k2,2 printed amy first while sort -k2 printed zed first.

Sorting tools companion · No. 02
the fence

The key range

-k2,2 ends where it should

where the key ends changes everythingzed bird aaadefault: the whole linezed bird aaa-k2,2: field two onlyzed bird aaa-k2: field two to the endsort -k2,2: amy firstsort -k2: zed firstsame file, opposite answers:write the end of the key-k2,2 starts and ends at field two; bare -k2 spans every field to the line's end

A key has a start and an end. Per the Wikipedia article, -k2,2n specifies sorting on the key starting and ending with column 2. Drop the end and -k2 spans all the fields in between, to the end of the line.

Our capture on pets.txt: with sort -k2,2 the bird keys tie and the whole line breaks it, amy first; with sort -k2 the key becomes bird aaa against bird zzz, and zed lands first.

Same file, opposite answers. Write the end of the key, always, and the surprise disappears.

ONE HONEST HOUR

Sort pets.txt both ways yourself, then write one sentence on why the two orders differ.

Per the Wikipedia Sorting algorithm article, stable sorting algorithms maintain the relative order of records with equal keys. Per the sort(1) manual page, -s, --stable stabilizes sort by disabling last-resort comparison. This machine's capture: sort -k1,1n pairs.txt printed 1 x, 1 y, 2 a, 2 b, while sort -s -k1,1n pairs.txt printed 1 y, 1 x, 2 b, 2 a.

Sorting tools companion · No. 03
the keeper

Stable order

-s keeps equal keys in place

equal keys, two different futuressort -k1,1n:1 x1 y2 a2 blast resort reordered the tiessort -s -k1,1n:1 y1 x2 b2 ainput order kept within equal keysstable sorts maintain the relative order of equal keys; -s disables last resort

When keys tie, something still decides. Per the Wikipedia Sorting algorithm article, stable sorting algorithms maintain the relative order of records with equal keys.

GNU sort breaks ties with a last-resort whole-line comparison; per the manual page, -s disables last-resort comparison and stabilizes the sort. Our capture: without -s, ties reorder to 1 x, 1 y; with sort -s -k1,1n, pairs keep input order: 1 y, 1 x, 2 b, 2 a.

Stability is the amber promise: your earlier care survives the later sorts.

ONE HONEST HOUR

Copy pairs.txt, run both sorts, and underline the rows that switched places when -s arrived.

Per the sort(1) manual page, -b, --ignore-leading-blanks ignores leading blanks, and if neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. This machine's capture: sort indent.txt printed the four-space apple line first and fig, kiwi before the two-space pear, while sort -b indent.txt ordered them apple, fig, kiwi, pear by name.

Sorting tools companion · No. 04
the trim

Blanks ignored

-b skips leading blanks

leading blanks are data to a plain sort$ sort -b indent.txt    apple  pearfigkiwiplain sort floats the indented lineswith -b, by name:apple, fig, kiwi, pearleading spaces still printedbut no longer countedcat -A shows the blanks as they are-b ignores leading blanks; without it, blank space sorts before every letter

Indentation is data to a plain sort: our capture shows sort indent.txt ranking blank space before every letter, so the indented lines float to the top.

Per the sort(1) manual page, -b ignores leading blanks. With it, the same file orders itself by name: apple, fig, kiwi, pear, the leading spaces still printed but no longer counted.

The manual page adds the fine print: without -t or -b, field characters count from the preceding whitespace. -b removes that drift.

ONE HONEST HOUR

Make an indented file with cat -A to see the spaces, sort it plain and with -b, and compare the first lines.

Per the Wikipedia uniq article, the command sort file | uniq -c | sort -n lists the unique lines in a file, sorted by the number of times each occurs, and using uniq like this is common when building pipelines in shell scripts. This machine's capture: sort dup.txt | uniq -c | sort -n printed 1 fig, 2 apple, 3 pear, the counts now in numeric order.

Sorting tools companion · No. 05
the pipeline

The count ladder

sort, count, sort again

sort, count, sort again1sort2uniq -c3sort -norder the lines, count the runs,order by the counts: three decisions,one line, straight from the article1 fig2 apple3 pearthe capture, smallest count firstsort file | uniq -c | sort -n: the counts climb, one to three

Here is the whole book in one line, straight from the Wikipedia uniq article: sort file | uniq -c | sort -n lists the unique lines, sorted by the number of times each occurs.

Our capture ran it: 1 fig, 2 apple, 3 pear, the census re-sorted by its own counts, smallest first.

Three decisions stacked: order, count, order by the count. The article notes this pattern is common when building pipelines in shell scripts.

ONE HONEST HOUR

Build the ladder over your duplicate file, then break one rung and predict the shape of the wreckage before you look.

Per the sort(1) manual page, -o, --output=FILE writes the result to FILE instead of standard output. Per the Wikipedia Sort (Unix) article history, sort retained an option to name the output file because it was used to sort a file in place, from the days of Version 4 Unix. This machine's capture: sort -n -o numbers_sorted.txt numbers.txt then cat printed 1, 2, 9, 10.

Sorting tools companion · No. 06
the destination

The -o output

write the result to a file

the destination is a file, not the screen$ sort -n -o numbers_sorted.txt numbers.txt-n-onumbers_sorted.txtwrites the result to FILEinstead of standard outputthe source file stayed as it wasthe scratch copy, catted:1 2 9 10an old idea: sort once sorteda file in place, so the optionto name the output survivedsort -n -o numbers_sorted.txt numbers.txt: result to the file, source untouched

The screen is not the only destination. Per the sort(1) manual page, -o writes the result to FILE instead of standard output.

It is an old idea: per the Wikipedia article's history, sort retained an option to name the output file because it was once used to sort a file in place.

Our capture, scratch copy only: sort -n -o numbers_sorted.txt numbers.txt, and the cat of the destination printed 1, 2, 9, 10. The source file stayed as it was.

ONE HONEST HOUR

Sort a file into a new scratch file with -o, cat both files, and confirm the original never changed.

Per this book's plan, the last lesson hands the tool to a friend in one honest hour: five lines re-read from this book's own captures, typed by the learner, read aloud by the teacher. Per the Wikipedia Sort (Unix) article, sort prints the lines of its input in sorted order and sorting is done based on one or more sort keys extracted from each line of input. This machine's capture: the five teaching lines are sort (GNU coreutils) 9.4, then 1, 10, 2, 9 against sort -n giving 1, 2, 9, 10, then sort -c unsorted.txt reporting disorder at line 3.

Sorting tools companion · No. 07
pass it on

Teaching sort

one honest hour, one friend

five lines, one friend, one hour1sort (GNU coreutils) 9.421, 10, 2, 931, 2, 9, 104disorder: b at line 353 pearthey type; you read eachdecision aloud, one by one;when they can explain why tenlanded before nine, it is theirsthe last decision is not a flag: pass the tool on in one honest hour

The last decision is not a flag: it is passing the tool on, one friend, one honest hour, per this book's plan.

They type; you read aloud. The five lines are this book's own captures: sort (GNU coreutils) 9.4, the text trap 1, 10, 2, 9, its cure 1, 2, 9, 10, the check report disorder at line 3, and the ladder's 3 pear.

When they can explain why ten landed before nine in their own words, the hour is done and the tool is theirs.

ONE HONEST HOUR

Sit with one friend and teach sort in one sitting; they type the five lines, you read each decision aloud.

Index

Index


Blanks ignored7
Reverse with -r4
Stable order6
Teaching sort10
The -o output9
The count ladder8
The key range5