sort (Unix)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

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.

Contents

$ls -s | sort -n
  96 Nov1.txt
 128 _arch_backup.lst
 128 _arch_backup.lst.tmp
1708 NMON

 $ cat phonebook
 Smith, Brett     555-4321
 Doe, John        555-1234
 Doe, Jane        555-3214
 Avery, Cory      555-4321
 Fogarty, Suzie   555-2314
   
 $ sort phonebook
 Avery, Cory      555-4321
 Doe, Jane        555-3214
 Doe, John        555-1234
 Fogarty, Suzie   555-2314
 Smith, Brett     555-4321

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 no longer supported, 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

 $ sort -t'|' -k2 zipcode
 Adam|12345
 Wendy|23456
 Bob|34567
 Sam|45678
 Joe|56789

The -r option just reverses the order of the sort:

 $ sort -nrk 2 zipcode
 Joe   56789
 Sam   45678
 Bob   34567
 Wendy 23456
 Adam  12345

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.