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 +1 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 +1 option causes sort to skip the first field of each line and start sorting at the second field (last name). However, 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 +1 -2 +0 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 +1 causes sort to skip the first field (first name) and sort by the second field (last name). The -2 causes the sort command to stop after comparing the second field. If any of the second fields are identical (in this case the Murray members) then the +0 instructs sort to make a second pass and sort from the beginning of the line, putting the first names in correct order.

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

% sort -n +2 -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 are sorting 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:


Tutlinks: intro1 intro2 wildcard permission pipe vi essential admin net fwall DNS diag Apache1 Apache2 MySQL1 MySQL2
Useful: Quiz Forums
Site Links:XMLZoo ActiveSQL ProgZoo SQLZoo