sort

>Usage

sort is a command that arranges the lines of a text file in a specified order. You can sort data alphabetically, numerically, or even reverse the order of the text. It’s like an automatic organizer that arranges your files in neat rows, whether by name, number, or other criteria.

>Synopsis

sort [option] [file]

>Options

-n

-n (numeric sort): Sorts lines based on numerical values rather than lexicographically.

Example 1:

Dots

-zsh

> echo -e "10\n2\n30" | sort -n


2
10
30

-r

-r (reverse order): Reverses the sorting order (sorts from high to low).

Example 2:

Dots

-zsh

> echo -e "apple\nbanana\ncherry" | sort -r


cherry
banana
apple

-k

-k (key): Sorts based on a specific field or column.

Example 3:

Dots

-zsh

> echo -e "apple 1\nbanana 3\ncherry 2" | sort -k2


apple 1
cherry 2
banana 3

-u

-u (unique): Removes duplicate lines after sorting.

Example 4:

Dots

-zsh

> echo -e "apple\nbanana\napple" | sort -u


apple
banana

-f

-f (ignore case): Ignores case when sorting.

Example 5:

Dots

-zsh

> echo -e "apple\nBanana\ncherry" | sort -f


apple
banana
cherry

-t

-t (delimiter): Specifies a delimiter for sorting fields.

Example 6:

Dots

-zsh

> echo -e "apple:1\nbanana:3\ncherry:2" | sort -t":" -k2


apple :1
cherry :2
banana :3

-b

-b (ignore leading blanks): Ignores leading spaces or tabs when sorting.

Example 7:

Dots

-zsh

> echo -e " apple\nbanana\n cherry" | sort -b


apple
banana
cherry

-M

-M (month sort): Sorts based on valid month abbreviation (e.g., three-letter abbreviations like Jan, Feb, Mar, etc.).

Example 8:

Dots

-zsh

> echo -e "Jan\nMar\nFeb" | sort -M


Jan
Feb
Mar

-V

-V (version sort): Sorts files or strings containing version numbers.

Example 9:

Dots

-zsh

> echo -e "file1.txt\nfile10.txt\nfile2.txt" | sort -V


file1.txt
file2.txt
file10.txt

-z

-z (zero-terminated): Treats input as zero-terminated rather than newline-terminated.

Example 10:

Dots

-zsh

> echo -e "apple\0banana\0cherry" | sort -z


apple
banana
cherry

>Quiz

Progress: Question ? out of ?

Question Title

Page written by Luca Fusetti