Sort (Unix)
From Wikipedia, the free encyclopedia
- The correct title of this article is sort (Unix). The initial letter is shown capitalized due to technical restrictions.
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. The -r flag will reverse the sort order.
Contents |
[edit] Examples
[edit] Sort the current directory by file size
$ls -s | sort -n 96 Nov1.txt 128 _arch_backup.lst 128 _arch_backup.lst.tmp 1708 NMON
[edit] Sort a file in alpha order
$ 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
[edit] Sort by number
The -n option makes the program to 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 to 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
[edit] 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
[edit] See also
[edit] External links
Unix command line programs and builtins (more) | |||
File and file system management: | cat | chattr | cd | chmod | chown | chgrp | cp | du | df | file | fsck | ln | ls | lsof | mkdir | mount | mv | pwd | rm | rmdir | split | touch | ||
Process management: | at | chroot | crontab | exit | kill | killall | nice | pgrep | pidof | pkill | ps | sleep | time | top | wait | watch | ||
User Management/Environment: | env | finger | id | mesg | passwd | su | sudo | uname | uptime | w | wall | who | whoami | write | ||
Text processing: | awk | comm | cut | ex | head | iconv | join | less | more | paste | sed | sort | tail | tr | uniq | wc | xargs | ||
Shell programming: | echo | expr | printf | unset | Printing: | lp |
Communications: inetd | netstat | ping | rlogin | traceroute |
Searching: find | grep | strings |
Miscellaneous: banner | bc | cal | man | size | yes |