Bash Sort Lines

sort reorders the lines of a file or pipeline alphabetically or numerically, and its flags control direction, uniqueness, and which column drives the comparison.

What Is sort?

sort reads every line of its input, orders them, and prints the result; by default it compares lines as plain text, character by character, in ASCII order. The examples below skip the header of team.csv with tail -n +2 so only the seven data rows are sorted.

Example

$ tail -n +2 team.csv | sort
Alice Johnson,Engineering,29,85000
Bob Smith,Marketing,34,62000
David Lee,Sales,26,54000
Eve Davis,Marketing,38,71000
Frank Miller,Engineering,45,110000
carol white,engineering,41,95000
grace hopper,Sales,31,67000

Numeric Sort with -n

Notice that carol and grace, whose names start with lowercase letters, sorted after every capitalized name above; in ASCII, uppercase letters come before lowercase ones. That same text comparison also breaks numbers, since the string 9 is considered greater than 54000 character by character. -n tells sort to compare fields as actual numbers, and -k combined with -t picks which delimited field to sort by, here the salary in column four.

Example

$ tail -n +2 team.csv | sort -t',' -k4 -n
David Lee,Sales,26,54000
Bob Smith,Marketing,34,62000
grace hopper,Sales,31,67000
Eve Davis,Marketing,38,71000
Alice Johnson,Engineering,29,85000
carol white,engineering,41,95000
Frank Miller,Engineering,45,110000

Reverse Order with -r and Deduplicating with -u

-r reverses whichever ordering is already active, numeric or alphabetical, so pairing it with -n produces a highest-to-lowest ranking. -u keeps only one line per unique sort key, dropping any further duplicates, which is a fast way to list the distinct values in a column once it has been isolated with cut.

Example

$ tail -n +2 team.csv | sort -t',' -k4 -nr
Frank Miller,Engineering,45,110000
carol white,engineering,41,95000
Alice Johnson,Engineering,29,85000
Eve Davis,Marketing,38,71000
grace hopper,Sales,31,67000
Bob Smith,Marketing,34,62000
David Lee,Sales,26,54000
$ tail -n +2 team.csv | cut -d',' -f2 | sort -u
Engineering
Marketing
Sales
engineering
  • Sort on a specific column with -k and -t, e.g. sort -t',' -k3 orders rows by the third comma-separated field.
  • -f, also written --ignore-case, folds case so carol and Carol are treated as equal during comparison.
  • -h compares human-readable sizes like 2K, 1M, and 3G in the order a person would expect, instead of alphabetically.
  • -c checks whether input is already sorted and reports an error without printing anything if it is not.
  • Chain a second key with another -k to break ties, e.g. sort -k2,2 -k4,4n sorts by department first and salary second.
FlagMeaning
-nCompares fields as numbers instead of text
-rReverses the sort order
-uKeeps only one line per unique sort key
-kSorts by a specific field instead of the whole line, e.g. -k4
-tSets the field separator used together with -k
Note: When you combine -u with -k, uniqueness is judged only on the sort key, not the whole line, so two rows with the same key but different other columns will still collapse into one.

Exercise: Bash Text Processing

What does "grep" do when searching a file for a pattern?