Syntax
Description
Thesort command arranges lines of text in order. By default, it performs ascending lexicographic (alphabetical) sorting. It’s essential for organizing data, finding patterns, and preparing input for commands like uniq.
Nash’s sort operates entirely in-memory and supports both regular and reverse sorting, with an option to remove duplicates during the sort process.
Options
Reverse the sorting order. Sort from Z to A, or highest to lowest.
Unique mode. Output only unique lines, removing adjacent duplicates after sorting.Equivalent to
sort | uniq.Examples
Basic alphabetical sort
Reverse sort
Sort with duplicates
Unique sort
Sort numbers (lexicographic)
Nash’s sort performs lexicographic (string) sorting. Numbers are sorted as text, so “10” comes before “2” because “1” < “2” alphabetically.
Sort file contents
Case sensitivity
Pipeline Examples
Sort then filter duplicates
Count occurrences
- Sort error messages
- Count each unique message
- Sort by count (descending)
Find common items
Top N items
Reverse unique sort
Practical Use Cases
Organize list of names
Deduplicate email list
Find duplicate entries
Prepare for uniq
uniq only removes adjacent duplicates, so sort first.
Alphabetize configuration
Find all unique words
Merge and sort files
Advanced Examples
Sort and reverse
Extract and sort unique fields
Sort by multiple criteria (simulated)
cut.
Remove blank lines and sort
Sort then process
Comparing Sort Options
- Default
- Reverse
- Unique
- Reverse Unique
Tips
Always sort before uniq
uniq only removes adjacent duplicates:
Use -u for efficiency
Instead of:Stable sort
Nash’s sort preserves the relative order of equal elements (stable sort).Empty lines
Empty lines sort to the beginning:Performance
Nash’s sort loads all input into memory, sorts it, then outputs the result. For very large datasets, consider processing in chunks or using the
-u flag to reduce output size.