sort
The sort command is useful for sorting lines. Let's use a small table of samples, with a sample name, a condition, and a count of reads, separated by tabs:
samples.tsv sample3 control 8000 sample1 control 12000 sample4 treated 45000 sample2 treated 9500
By default sort works alphabetically on the whole line:
$ sort samples.tsv sample1 control 12000 sample2 treated 9500 sample3 control 8000 sample4 treated 45000
You can reverse it with -r:
$ sort -r samples.tsv sample4 treated 45000 sample3 control 8000 sample2 treated 9500 sample1 control 12000
Now the interesting part. Say you want the samples ordered by read count, which is the third column. Use -k to pick the column:
$ sort -k 3 samples.tsv sample1 control 12000 sample4 treated 45000 sample3 control 8000 sample2 treated 9500
That is not what you wanted. 12000 and 45000 both came before 8000, because sort compared the text character by character, and the characters "1" and "4" sort before "8". Alphabetical order and numerical order are not the same thing.
-n tells sort to read the field as a number:
$ sort -n -k 3 samples.tsv sample3 control 8000 sample2 treated 9500 sample1 control 12000 sample4 treated 45000
That is the one you wanted. Forgetting -n on numbers is an easy mistake to make and a hard one to notice, because the output still looks sorted.
Combine it with -r to get the largest first, which is the usual way to answer "what are the top few":
$ sort -nr -k 3 samples.tsv sample4 treated 45000 sample1 control 12000 sample2 treated 9500 sample3 control 8000
Two more that come up often: -u drops duplicate lines as it sorts, and -h understands human readable sizes such as 4K and 2G, which is what you want for sorting the output of du.
Exercise
- Create the samples.tsv file above and sort it on the third column, with and without -n, and compare.
- Use sort -nr to put the largest count first.
- Try du -h in a directory with some files, and sort it with sort -h.
Quiz Question
What flag makes sort compare fields as numbers rather than as text?
Show answer
-n