If you can see this check that

next section prev section up prev page next page

Ordering files using sort

You can use the sort command to order the contents of files, sorting alphabetically, numerically, or by different fields. The sort command displays sorted file contents on a line-by-line basis. It compares the first characters in each line; if they are the same, it compares the next characters in turn, and so on through to the end of the line. For example, create the following file 'club_members'. Each line in the file has 5 fields: the first name, the last name, membership no, telephone no, and meetings attended. The fields are separated by spaces or tabs.

% cat > club_members
Pam Murray      4    657-4324  18
Jim Brown       8    467-8743  22
Jim Roberts     17   728-8295  9
Joyce Murray    7    235-1432  13
CTRL-D           (press and hold CONTROL and press D)
To display the sorted contents of the file, type:
% sort club_members
Jim Brown       8    467-8743  22
Jim Roberts     17   728-8295  9
Joyce Murray    7    235-1432  13
Pam Murray      4    657-4324  18

Notice that the list is sorted alphabetically by first names. The sort command without any options will sort starting with the first field in each line. The sort command places the two 'Jim' members in the correct order, because after comparing the first names and finding them identical, it compares the second fields. The following example shows how you can use sort to order by last name.

% sort -k 2,2 club_members
Jim Brown       8    467-8743  22
Pam Murray      4    657-4324  18
Joyce Murray    7    235-1432  13
Jim Roberts     17   728-8295  9

The "-k 2,2" option causes sort to sort using start in column 2 (the last name) and to end in column 2 (otherwise it uses all columns from 3 to the end). Note that the first names Pam and Joyce are not in alphabetical order.

The next example shows how to sort by last name and then by first name if the last names are identical.

% sort -k 2,2 -k 1,1 club_members
Jim Brown       8    467-8743  22
Joyce Murray    7    235-1432  13
Pam Murray      4    657-4324  18
Jim Roberts     17   728-8295  9
The 2,2 means the first key is only column 2, and if they are identical then try using 1,1 which means only column 1.

Suppose now you wanted to sort the file by membership numbers:

% sort -k 3n,3 club_members
Pam Murray      4    657-4324  18
Joyce Murray    7    235-1432  13
Jim Brown       8    467-8743  22
Jim Roberts     17   728-8295  9
Now, since we want to sort in numerical order, the numbers will not be in the right order unless we use the "n" (numeric) option, since by default sort works by comparing characters in a field in the order they appear. So without the n option, 17 would come before 9, as 9 is larger than 1 (the first character of 17).

Common option of the sort command are:


Centos 7 intro: Paths | BasicShell | Search
Linux tutorials: intro1 intro2 wildcard permission pipe vi essential admin net SELinux1 SELinux2 fwall DNS diag Apache1 Apache2 log Mail
Caine 10.0: Essentials | Basic | Search | Acquisition | SysIntro | grep | MBR | GPT | FAT | NTFS | FRMeta | FRTools | Browser | Mock Exam |
CPD: Cygwin | Paths | Files and head/tail | Find and regex | Sort | Log Analysis
Kali: 1a | 1b | 1c | 2 | 3 | 4a | 4b | 5 | 6 | 7a | 8a | 8b | 9 | 10 |
Kali 2020-4: 1a | 1b | 1c | 2 | 3 | 4a | 4b | 5 | 6 | 7 | 8a | 8b | 9 | 10 |
Useful: Quiz | Forums | Privacy Policy | Terms and Conditions

Linuxzoo created by Gordon Russell.
@ Copyright 2004-2023 Edinburgh Napier University