Sort ( Unix ) - wikipedia Sort ( Unix ) In Unix - like operating systems, sort is a standard 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 the default field separator. The `` - r '' flag will reverse the sort order. Contents 1 History 2 Examples 2.1 Sort a file in alphabetical order 2.2 Sort by number 2.3 Columns or fields 2.4 Sort on multiple fields 2.5 Sorting a pipe delimited file 2.6 Sorting a tab delimited file 2.7 Sort in reverse 2.8 Sort in random 3 Sorting algorithm 4 See also 5 References 6 External links History Sort was part of Version 1 Unix. By Version 4 Ken Thompson had modified it to use pipes, but sort retained an option to name the output file because it was used to sort a file in place. In Version 5, Thompson invented `` - '' to represent standard input. Examples Sort a file in alphabetical order $ cat phonebook 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 Sort by number The - n option makes the program sort according to numerical value. The du command produces output that starts with a number, the file size, so its output can be piped to sort to produce a list of files sorted by ( ascending ) file size : $ du / bin / * sort - n 4 / bin / domainname 24 / bin / ls 102 / bin / sh 304 / bin / csh Columns or fields Use the - k option to sort on a certain column. For example, use `` - k 2 '' to sort on the second column ). In old versions of sort, the + 1 option made the program sort on the second column of data ( + 2 for the third, etc. ). This usage is deprecated. $ cat zipcode Adam 12345 Bob 34567 Joe 56789 Sam 45678 Wendy 23456 $ sort - k 2n zipcode Adam 12345 Wendy 23456 Bob 34567 Sam 45678 Joe 56789 Sort on multiple fields The - km, n option lets you sort on a key that is potentially composed of multiple fields ( start at column m, end at column n ) : $ cat quota fred 2000 bob 1000 an 1000 chad 1000 don 1500 eric 500 $ sort - k2n, 2 - k1, 1 quota eric 500 an 1000 bob 1000 chad 1000 don 1500 fred 2000 Here the first sort is done using column 2. - k2, 2 specifies sorting on the key starting and ending with column 2. If - k2 is used instead, the sort key would begin at column 2 and extend to the end of the line, spanning all the fields in between. The n stands for ' numeric ordering '. - k1, 1 dictates breaking ties using the value in column 1, sorting alphabetically by default. Note that bob, an and chad have the same quota and are sorted alphabetically in the final output. Sorting a pipe delimited file $ sort - k2, 2, - k1, 1 zipcode Adam 12345 Wendy 23456 Sam 45678 Joe 56789 Bob 34567 Sorting a tab delimited file Sorting a file with tab separated values requires a tab character to be specified as the column delimiter. This illustration uses the shell 's dollar - quote notation to specify the tab as a C escape sequence. $ sort - k2, 2 - t $ ' \t ' phonebook Doe, John 555 - 1234 Fogarty, Suzie 555 - 2314 Doe, Jane 555 - 3214 Avery, Cory 555 - 4132 Smith, Brett 555 - 4321 Sort in reverse The - r option just reverses the order of the sort : $ sort - rk 2n zipcode Joe 56789 Sam 45678 Bob 34567 Wendy 23456 Adam 12345 Sort in random The GNU implementation has a - R / -- random - sort option based on hashing ; this is not a full random shuffle because it will sort identical lines together. A true random sort is provided by the Unix utility shuf. Sorting algorithm The implementation in GNU Core Utilities, used on Linux, employs the merge sort algorithm. See also Collation List of Unix programs uniq shuf References Jump up ^ McIlroy, M.D. ( 1987 ). A Research Unix reader : annotated excerpts from the Programmer 's Manual, 1971 -- 1986 ( PDF ) ( Technical report ). CSTR. Bell Labs. 139. Jump up ^ `` The GNU Bash Reference Manual, for Bash, Version 4.2 : Section 3.1. 2.4 ANSI - C Quoting ''. Free Software Foundation, Inc. 28 December 2010. Retrieved 1 February 2013. Words of the form $ ' string ' are treated specially. The word expands to string, with backslash - escaped characters replaced as specified by the ANSI C standard. Jump up ^ Fowler, Glenn S. ; Korn, David G. ; Vo, Kiem - Phong. `` KornShell FAQ ''. Archived from the original on 2013 - 05 - 27. Retrieved 3 March 2015. The $ '... ' string literal syntax was added to ksh93 to solve the problem of entering special characters in scripts. It uses ANSI - C rules to translate the string between the '... '. External links Sort manpage The program 's manpage Softpanorama Unix sort page Unix command - line interface programs and shell builtins File system cat chmod chown chgrp cksum cmp cp dd du df file fuser ln ls mkdir mv pax pwd rm rmdir split tee touch type umask Processes at bg crontab fg kill nice ps time User environment env exit logname mesg talk tput uname who write Text processing awk basename comm csplit cut diff dirname ed ex fold head iconv join m4 more nl paste printf sed sort strings tail tr uniq vi wc xargs Shell builtins alias cd echo test unset wait Searching find grep Documentation man Software development ar ctags lex make nm strip yacc Miscellaneous bc cal expr lp od sleep true and false Categories Standard Unix programs Unix SUS2008 utilities List Retrieved from `` https://en.wikipedia.org/w/index.php?title=Sort_(Unix)&oldid=832716540 '' Categories : Sorting algorithms Unix text processing utilities Unix SUS2008 utilities Talk Contents About Wikipedia Беларуская Čeština Deutsch Español Français 한국어 Italiano עברית Magyar 日本 語 Polski Português Română Русский Српски / srpski 6 more Edit links This page was last edited on 27 March 2018, at 16 : 24. About Wikipedia

Question: unix command to sort the contents of a file
Answer: sort