Sunday, May 16, 2010

Sort command in Linux

To do a descending numeric sort on the 4th column of a tab-separated file qwe.txt, do
sort -t$'\t' -rn -k4,4 qwe.txt > sorted.qwe.txt

Note the grouping: sort [separator] [options] [fields to sort on]
Here I have used the options: -r (descending order) and -n (numeric sort).

The trick for specifying tab as a field separator is borrowed from here. Also see this.