Sort Command (Linux)

Options:

[1] -u:   with -c, check for strict ordering; without -c, output only the first of an equal run

[2] -n: compare according to string numerical value.  Note, without using the -n option, the sorting will be on ASCII

$ cat file
20
19
5
49
200

$ sort file
19
20
200
49
5

[3] -r:  reverse the result of comparisons

[4] -k: sort via a key; KEYDEF gives location and type

[5] -t :  use SEP instead of non-blank to blank transition

Examples:

$ cat file1.txt
20
19
5
49
200

$ cat file2.txt
25
18
5
48
200

$ sort -n file1.txt file2.txt
5
5
18
19
20
25
48
49
200
200
$ sort -nu file1.txt file2.txt
5
18
19
20
25
48
49
200

Note : how the above examples are actually working on multiple files.

 

Files with multiple fields and delimiter

$ cat file

Linux,20

Unix,30

AIX,25

Linux,25

Solaris,10

HPUX,100

  • Sort the file alphabetically on the 1st field, numerically on the 2nd field:

    $ sort -t”,” -k1,1 -k2n,2 file

    AIX,25

    HPUX,100

    Linux,20

    Linux,25

    Solaris,10

    Unix,30

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s