next up previous contents
Next: Displaying the last few Up: Other useful commands Previous: Using find for useful   Contents

Sorting files

The command sort <filenames> sorts files alphabetically by line. Sort considers each line as a record with space-separated fields. By default the first field is used to sort the file. The sort command has many options: sorting numerically, sorting in reverse order, sorting on fields within the line etc. Please check the man page for more options.

Here are some common options:

sort -r file1        reverse normal order
sort -n              sort in numeric order
sort -rn             sort in reverse numeric order
sort -f              fold upper and lower case together
sort +n              sort starting at n+1st field 
sort -k 3,3 --stable sort on the third field using a stable sorting algorithm

sort datafile | uniq -c   sort file and print unique values with duplicate counts



Amit Jain 2013-04-22