shellspells beta

sort

sort [OPTION]... [FILE]...

sort prints the lines of its input or concatenation of all files listed in its argument list in sorted order.

An essential utility for sorting. Often used in conjunction with the uniq command to provide a distinct list of elements (see below).

Examples

To sort in numeric order, you must pass the -n flag, or you get unexpected results

$ head -10 aliens_release.txt | sort
10|Spain|27 October 1986|
1|Canada|18 July 1986|
2|USA|18 July 1986|
3|Japan|09 August 1986|
4|Brazil|28 August 1986|
5|UK|29 August 1986|
6|Argentina|11 September 1986|
7|Italy|30 September 1986|
8|Sweden|03 October 1986|
9|France|08 October 1986|

$ head -10 aliens_release.txt | sort -n
10|Spain|27 October 1986|
1|Canada|18 July 1986|
2|USA|18 July 1986|
3|Japan|09 August 1986|
4|Brazil|28 August 1986|
5|UK|29 August 1986|
6|Argentina|11 September 1986|
7|Italy|30 September 1986|
8|Sweden|03 October 1986|
9|France|08 October 1986|

Sort a directory based on size

ls -s | sort -n

Setting the field separator to TAB and sorting the second column

head -5 aliens_release.txt | sort -t'|' -k2
4|Brazil|28 August 1986|
1|Canada|18 July 1986|
3|Japan|09 August 1986|
5|UK|29 August 1986|
2|USA|18 July 1986|

Get a list of distinct users logged into a system

who | nawk '{print $1}' | sort | uniq