Sort (Unix)
Encyclopedia
sort is a standard Unix
command line program that prints the lines of its input or concatenation of all files listed in its argument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key. Blank space is taken used as default field separator.
The -r flag will reverse the sort order.
96 Nov1.txt
128 _arch_backup.lst
128 _arch_backup.lst.tmp
1708 NMON
Smith, Brett 555-4321
Doe, John 555-1234
Doe, Jane 555-3214
Avery, Cory 555-4132
Fogarty, Suzie 555-2314
$ sort phonebook
Avery, Cory 555-4132
Doe, Jane 555-3214
Doe, John 555-1234
Fogarty, Suzie 555-2314
Smith, Brett 555-4321
$ cat quota
bob 1000
an 1000
chad 1000
don 1500
eric 5000
fred 2000
$ sort -k2n,2 -k1,1 quota
an 1000
bob 1000
chad 1000
don 1500
fred 2000
eric 5000
$ du /bin/* | sort -n
4 /bin/domainname
24 /bin/ls
102 /bin/sh
304 /bin/csh
In old versions of sort, the +1 option made the program sort using the second column of data (+2 for the third, etc.). This is deprecated, and instead the -k option can be used to do the same thing (note: "-k 2" for the second column):
$ cat zipcode
Adam 12345
Bob 34567
Joe 56789
Sam 45678
Wendy 23456
$ sort -nk 2 zipcode
Adam 12345
Wendy 23456
Bob 34567
Sam 45678
Joe 56789
Adam|12345
Wendy|23456
Bob|34567
Sam|45678
Joe|1
$ sort -nrk 2 zipcode
Joe 56789
Sam 45678
Bob 34567
Wendy 23456
Adam 12345
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
command line program that prints the lines of its input or concatenation of all files listed in its argument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key. Blank space is taken used as default field separator.
The -r flag will reverse the sort order.
Sort the current directory by file size
$ ls -s | sort -n96 Nov1.txt
128 _arch_backup.lst
128 _arch_backup.lst.tmp
1708 NMON
Sort a file in alpha order
$ cat phonebookSmith, Brett 555-4321
Doe, John 555-1234
Doe, Jane 555-3214
Avery, Cory 555-4132
Fogarty, Suzie 555-2314
$ sort phonebook
Avery, Cory 555-4132
Doe, Jane 555-3214
Doe, John 555-1234
Fogarty, Suzie 555-2314
Smith, Brett 555-4321
Sort on multiple fields
The -k m,n option lets you sort on a particular field (start at m, end at n):$ cat quota
bob 1000
an 1000
chad 1000
don 1500
eric 5000
fred 2000
$ sort -k2n,2 -k1,1 quota
an 1000
bob 1000
chad 1000
don 1500
fred 2000
eric 5000
Sort by number
The -n option makes the program sort according to numerical value:$ du /bin/* | sort -n
4 /bin/domainname
24 /bin/ls
102 /bin/sh
304 /bin/csh
In old versions of sort, the +1 option made the program sort using the second column of data (+2 for the third, etc.). This is deprecated, and instead the -k option can be used to do the same thing (note: "-k 2" for the second column):
$ cat zipcode
Adam 12345
Bob 34567
Joe 56789
Sam 45678
Wendy 23456
$ sort -nk 2 zipcode
Adam 12345
Wendy 23456
Bob 34567
Sam 45678
Joe 56789
Sorting a pipe delimited file
$ sort -t'|' -k2 zipcodeAdam|12345
Wendy|23456
Bob|34567
Sam|45678
Joe|1
Sort in reverse
The -r option just reverses the order of the sort:$ sort -nrk 2 zipcode
Joe 56789
Sam 45678
Bob 34567
Wendy 23456
Adam 12345