sort

sort lines of text files

Syntax

sort [OPTION]... [FILE]...

Description

Write sorted concatenation of all FILE(s) to standard output.

Options

-b, --ignore-leading-blanks
       ignore leading blanks

-d, --dictionary-order
       consider only blanks and alphanumeric characters

-f, --ignore-case
       fold lower case to upper case characters

-g, --general-numeric-sort
       compare according to general numerical value

-i, --ignore-nonprinting
       consider only printable characters

-M, --month-sort
       compare (unknown) < ‘JAN’ < ... < ‘DEC’

-h, --human-numeric-sort
       compare human readable numbers (e.g., 2K 1G)

-n, --numeric-sort
       compare according to string numerical value

-R, --random-sort
       sort by random hash of keys

--random-source=FILE
       get random bytes from FILE

-r, --reverse
       reverse the result of comparisons

--sort=WORD
       sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V

-k, --key=POS1[,POS2]
       start a key at POS1 (origin 1), end it at POS2 (default end of line)

-m, --merge
       merge already sorted files; do not sort

-o, --output=FILE
       write result to FILE instead of standard output

-s, --stable
       stabilize sort by disabling last-resort comparison

-t, --field-separator=SEP
       use SEP instead of non-blank to blank transition

-T, --temporary-directory=DIR
       use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories

-u, --unique
       with -c, check for strict ordering; without -c, output only the first of an equal run

-z, --zero-terminated
       end lines with 0 byte, not newline

Example 1

Sort the content of a file and display on standard output.

[eleiss@eleiss ~]$ cat cities 
Surat
Vadodara
Mumbai
Ahmedabad
Delhi
[eleiss@eleiss ~]$ sort cities 
Ahmedabad
Delhi
Mumbai
Surat
Vadodara
[eleiss@eleiss ~]$

In this example the content of the file "cities" is sorted in alphabetical order and then displayed on the standard output. By default the sort command sorts in alphabetical order.

The original content of the file remains untouched.

Example 2

Sort the content of a file in numeric order and display the content on standard output.

[eleiss@eleiss ~]$ cat directory
9009909090 J'lo
8008890765 Alizee
6789678543 Madonna
9087612111 Shakira
8887776115 Rihana
[eleiss@eleiss ~]$ sort -n directory
6789678543 Madonna
8008890765 Alizee
8887776115 Rihana
9009909090 J'lo
9087612111 Shakira
[eleiss@eleiss ~]$

In this example, the sort command sorts the content of the file in numeric order and displays the output on the standard output.

Example 3

Sort the content of a file in reverse alphabetical order and display the output on standard output.

[eleiss@eleiss ~]$ cat cities 
Surat
Vadodara
Mumbai
Ahmedabad
Delhi
[eleiss@eleiss ~]$ sort -r cities 
Vadodara
Surat
Mumbai
Delhi
Ahmedabad
[eleiss@eleiss ~]$

In this example, first the content of the file "cities" is displayed using cat command. Later the content of the same is sorted in reverse alphabetical order using sort command.

Example 4

Sort the content of a file in alphabetical order ignoring the case and display the unique lines to the standard output.

[eleiss@eleiss ~]$ cat cities 
Surat
vadodara
Mumbai
Ahmedabad
Delhi
surat
Vadodara
mumbai
ahmedabad
delhi
[eleiss@eleiss ~]$ cat cities  | sort -fu
Ahmedabad
Delhi
Mumbai
Surat
vadodara
[eleiss@eleiss ~]$

In this example, first the content of the file "cities" is displayed using cat command. Later the content of the same is sorted in alphabetical order using sort command. The -f option ignores the case and -u option displays only unique lines.

Example 5

Sort content of a file in ascending order according to months.

[eleiss@eleiss ~]$ cat birth_info 
Jul,1969 J'lo
Aug,1984 Alizee
Aug,1958 Madonna
Feb,1977 Shakira
Feb,1983 Rihana
[eleiss@eleiss ~]$ sort -M birth_info 
Feb,1977 Shakira
Feb,1983 Rihana
Jul,1969 J'lo
Aug,1958 Madonna
Aug,1984 Alizee
[eleiss@eleiss ~]$

The file "birth_info" contains the birth year and month of the popular celebrities. The content of the file is displayed using cat command first. Later the same content is displayed sorted in ascending order according to the months on standard output using sort command.

Example 6

Sort the content of a file in alphabetical order according to some field in the file.

[eleiss@eleiss ~]$ cat birth_info 
Jul,1969 J'lo
Aug,1984 Alizee
Aug,1958 Madonna
Feb,1977 Shakira
Feb,1983 Rihana
[eleiss@eleiss ~]$ sort -t " " -k 2 birth_info 
Aug,1984 Alizee
Jul,1969 J'lo
Aug,1958 Madonna
Feb,1983 Rihana
Feb,1977 Shakira
[eleiss@eleiss ~]$

The file "birth_info" is a two fields file. The fields are separated by a space character. The sort command sorts the content of the file in alphabetical order but according to the second field.


You may write to resources@eleiss.com for any Linux related queries.

advertisement