|
|
You can sort a file containing lines of text or numerical data in a variety of ways using the sort(C) command. For example, suppose you have a file called names containing the following:
perry john sarah charlesTo sort its contents alphabetically, enter the following command:
$ sort names charles john perry sarahTo direct the sorted output to a file (names1) rather than the screen (standard output), you can use either of the following command lines:
$ sort -o names1 names $ sort names > names1You can cause the original file to be sorted by giving the original filename for both arguments.
You can make sort merge two files together, in order. To
do this, type the following:
sort filename1 filename2 > filename3
This creates filename3, which contains the sorted, merged contents of filename1 and filename2. (The sort command sorts the files as it merges them.) You can use the -u option to tell sort to make sure that each line in filename3 is unique; that is, if both filename1 and filename2 contain an identical line, only one copy of the line will be written to filename3:
$ cat file1 perry john sarah charles $ cat file2 susanna charles bridget johnRunning the sort command on these files merges the contents and places them in alphabetic order, as follows:
$ sort -u file1 file2 >file3 $ cat file3 bridget charles john perry sarah susannaThere are several more options that can be used with sort. For example, -r sorts in reverse order; -n sorts on numerical order, not text order; -M causes sort to assume that the first three characters of the field being sorted are months (like ``JAN'', ``FEB'', ``MAR'', and so on) and sorts them into date order.
You can make sort select any field in a line and have it base its comparisons on that field, as follows:
$ cat birthdays charles FEB bridget DEC sarah JAN $ sort -M +1 birthdays sarah JAN charles FEB bridget DECThe +1 flag tells sort to make comparisons between records on the basis of the second field of each line. So, the month abbreviation on each line of the file is used as the basis for the sort operation above, and not the alphabetic order of the first field.
If you have a file where data records are made up of fields separated by some special character (called a ``separator''), you can tell sort to use that separator by using the -t option, as follows:
$ cat birthdays charles:FEB bridget:DEC sarah:JAN $ sort -M +1 -t: birthdays sarah:JAN charles:FEB bridget:DEC