Home > Text Processing & Search > sort

sort: The Standard for Sorting Text Lines

Sorts text lines (records). The default is lexicographical (byte-order) ascending, supporting various criteria such as delimiters, keys, numbers, versions, and human-readable units. Based on GNU coreutils' sort.

Overview

Receives files or standard input, sorts them line by line, and sends the result to standard output.

Basic Usage

sort input.txt > output.txt

Default lexicographical ascending sort

Key Points

  • Uses standard input if no input is specified (often used with pipes)
  • Key sorting combines -t (delimiter) and -k (field/character range)
  • Provides dedicated modes for numbers, human-readable units, versions, etc.

Key Options

Options related to sorting criteria, output control, and performance.

Sorting Criteria

Output/Behavior Control

Performance/Resources

Help/Version

Generated command:

Try combining the commands.

Description:

`sort` Executes the command.

Combine the above options to virtually execute commands with AI.

Common Patterns

Numeric Sort + Reverse

sort -n -r scores.txt

Descending order based on numeric values

Sort by 3rd Column (Numeric) in CSV

sort -t, -k3,3n data.csv

Specify delimiter with -t, key range with -k

Sort Sizes by Human-Readable Units

du -h /var/log | sort -h

Sorts formats like `ls -lh` output

Stable Sort by First Column After Removing Duplicates

sort -s -k1,1 -u users.txt

Combines stable (-s) and -u

Key Range Notation Summary POS is field.character index (1-based).
Notation Meaning
-k2,2 Uses the entire 2nd field as the key
-k2.3,2.5 Uses only characters 3-5 within the 2nd field
-k1,1 -k2,2 Primary sort by 1st field, then secondary sort by 2nd field if primary keys are equal

Related commands

These are commands that are functionally similar or are commonly used together.



Same category commands