The Sort command

The sort command is a little more complex. For the sort purpose of this section, I'd like you to open one terminal in which you can do an info OR man of the sort command and another in which you can run the examples.

I'm going to cover sort in a fairly basic manner. It can get a lot more complex than what we're covering here, but for the most part you're going to use it in a fairly basic manner. If you need to do more with it, then by all means read the info page.

Sort takes a number of command line arguments. First it takes the field that you're sorting on. Fields are counted from 0 upwards. So if we had a line with a number of different fields, separated by spaces:

f1 f2 f3 f4 
            

then the fields would be numbered as follows:

0  1  2  3 
            

Sort can be called to sort on a particular field(s). sorting on field 0:

sort 0
            

Leaving the 0 off implies that the sort happens on the 0'th (first) field by default.

Previously we did:

cut -d: -f7 /etc/passwd | sort | uniq
            

which is the same as

cut -d: -f7 /etc/passwd | sort -u
            

since sort's -u parameter is equivalent to running the output to the uniq program. This means that we've now cut down on the number of commands that we require.

Remember in the shell, every time we run a command, it has to invoke the ommand, which implies a time delay.

We might say we want to reverse sort using the -r switch.

cut -d: -f7 /etc/passwd |sort -ur
            

or

cut -d: -f7 /etc/passwd |sort -ru 
            

This would uniquely sort things and it would reverse the sort. If we wanted to output this to a file, we could redirect it to uniq_shells.txt:

cut -d: -f7 /etc/passwd |sort -ru > uniq_shells.txt 
            

We could use the equivalent method of using -o switch which would remove the need for the redirect symbol:

cut -d: -f7 /etc/passwd |sort -ruo uniq_shells.txt 
            

Let's work a little bit more with our password file. I'm interested in the UID of all our users. Our password file (fields are separated by colons rather than spaces), can be sorted as follows:

f1        :f2:f3  :f4  :f5: ...:f7
uname :x :uid :gid     :... :/bin/bash
            

I want the username, the userid and the shell (fields 1,3 and 7 from /etc/passwd). So:

cut -d: -f1,3,7 /etc/passwd
            

This output of this cut command should be in the format of:

username:userid:shell
            

How do we sort this by userid? [9]

We want to sort on the userid, which is the second field in our list, but is referred to as field 1 (since the fields in sort start from 0).

cut -d: -f1,3,7 /etc/passwd |sort -t: +1n
            

Sort now includes a switch +1, since we want to sort 'one-field-away' from field 0. We also want to make this a numeric sort (using the n switch) and we are using a colon delimiter.

Another example:

df -k |tr -s ' ' |sort +4n 
            

IS DIFFERENT to

df -k |tr -s ' ' |sort +4
            

Here sort is now sorting the 5th field assuming it is an alphanumeric and not a numeric. Here we are sorting on which field? That's right, the percent used field.

How do we skip fields? We use a +1 or a +4 to skip one or four fields respectively. You can combine these switches as we've done (using -t and -n).

On our password example, we may want to reverse sort thereby putting the root user id at the bottom of the sort:

cut -d: -f1,3,7 /etc/passwd |sort -t: +1rn
            

where the -r switch forces a reverse sort.

This is a short summary of some of the options available with the sort command:

option action
-o for output to a file (also can be substituted with >)
-u to do unique sorts
+t to specify the delimiter
+3 indicating how many fields we want to skip
-n to do numeric sorts as opposed to alphanumeric sorts
-r to reverse sorts

There are a lot of other sort switches, and I encourage you to look at the info page for the sort command .

Finally. You will have noticed that Linux allows us to combine switches. So, instead of typing the sort as:

	
sort -t: +1 -r -n
            

we could do it as

	
sort -t: +1rn
            


[9] Note that the delimiter switch for the sort command is a t not a d.