Sort (Unix)


sort --help

sort is a command line utility for the Unix operating system.

It works as follows: it takes the files listed in its argument list and sorts its lines. The ordering is done on the basis of one or more keys extracted from each line of the input files. By default, all input data is taken as the sort key. Blanks are taken by default as field separators. The -r parameter will reverse the order.

Use: $ sort [options] [file] Examples

To sort the current directory by file size: $ ls -s|sort -n 0 GNUstep 0 Mail 4 ALT.txt 4 Events 4 Ted.lnk 12 stats 124 _backup

To sort a list in alphabetical order: $ cat agenda.txt Juan López 555-4321 Antonia Pérez 555-1234 Rodolfo Ruiz 555-3214 Ana Cohen 555-4321 $ sort agenda.txt Ana Cohen 555-4321 Antonia Pérez 555-1234 Juan López 555-4321 Rodolfo Ruiz 555-3214

The -n option makes the order according to numeric values: $ du / bin / * | black -n 10 / bin / domainname 10 / bin / hostname 10 /bin/run-parts 42 /bin/cp 675 /bin/bash

In the old versions of the command there was the option +1, which indicated a sort to be ordered by taking the second column of data (using +2 for the third and so on). The most current versions do not support this option, instead the -k option is used, for example, to sort using the second column of data as a criterion: $ cat ''edades'' Francisco 45 Javier 56 Lola 34 Jorge 25 $ sort -nk 2 ''edades'' Jorge 25 Lola 34 Francisco 45 Javier 56

Sort a file using tabs: $ sort -t'|' -k2 ''edades'' Jorge|25 Lola|34 Francisco|45 Javier|56

The -r option reverses the order: $ black-no 2 '' edades '' Javier 56 Francisco 45 Lola 34 Jorge 25

wiki