Tuesday, July 31, 2001

Sorting files

Some times we save the data in certain order but we would like to read it in another order. I mean, we want to sort it :-) So here comes the sort command to the rescue…


This will sort the file and list but will not save it

To save this

sort -o

This will save the output.

Needless to say this last example will overwrite the unsorted (original) file, so be careful when you use it.

You can sort things other than files. What I mean is that you can use sort to sort the output of other commands. For example, if you want to know the disk usage of all your directories you can try something like the following:

du -s * | sort -n

The du -s * tells the computer to find the disk usage of each file/subdirectory (and do not give the output of sub-subdirectories…, just a number per file/directory). Then the vertical line (called pipe) says that the output of du should be considered as the input of the next command. That next command tells to sort numerically. Since du output is a number (disk usage) and the name of the file, the sort will order the directories according to the disk space they use

By default sort uses blanks (spaces, tabs, etc) to differentiate between fields/words. Some times files store data with other characters to separate fields, for example the /etc/passwd file stores uses information with different fields separated by colons (:). You can use the -t option to sort with colons as the separator. For example, entries in the passord file have the following form:

user name: encrypted password:user id:group it:user real name:home directory:logging shell

If you want to sort the password by UIDs then you can try this:

sort -t : +2 -n /etc/passwd

Here is the explanation of the options:
  1. -t : says that the separation of fields is marked by colons;
  2. +2 says that sorting should start at the third field, that is the uid
  3. -n asks for numerical sorting (uids are numbers).