View the Most Wanted LQ Wiki articles.
LinuxQuestions.org > Linux Wiki > Sort

From LQWiki

Jump to: navigation, search

The sort command sorts lines of text. It accepts a file or list of files to sort, and if none or '-' is given, it reads from stdin.

Example usage

$ echo -e "b\nc\na" |sort
a
b
c

Some of the most useful options:

-f ignores case
-k sorts on a key (whitespace separated text-block)
-n sorts numbers
-r reverses sort
-u unique lines - sometimes makes a pipe to uniq unnecessary

$ cat sortdemo                                                           
foo 100
foo 95
bar 12
Foo 200
baz 156
Foo 100
$ sort -fru sortdemo                                                      
foo 95
Foo 200
foo 100
baz 156
bar 12 
$ sort -nk2 sortdemo                                                      
bar 12
foo 95
Foo 100
foo 100
baz 156
Foo 200

Personal tools