Sometime we need to sort a file on a specific field. This may be useful if a file is in csv or tsv formart (probably an export from excel) and we need to sort it on a specific fields or columns. Here are some handy sort command on Linux for sorting on specific fields.
First we’ll set environment variable LC_ALL=C to ensure we use simple byte comparison for sorting:
$ LC_ALL=C
Basic sort
To do the basic sort run the following command:
$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort aa bb 11 cc mm nn 2 pp
Sort starting from a specific field
To sort the input starting from third field onwards and use space as delimiter, run the following command:
$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3 aa bb 11 cc dd 2
To sort only using 3rd field, you can use the following command:
$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3,3 aa bb 11 cc dd 2
Numeric sort starting from a specific field
To do numeric sort from a specific field, add -n
as shown below:
$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -n -k3 mm nn 2 pp aa bb 11 cc
You can also use the following alternate format:
$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3n mm nn 2 pp aa bb 11 cc
Reverse numeric sort on a specific field
To do reverse numeric sort from a specific field, add -n
and -r
as shown below:
$ printf "aa bb 11 cc\nmm nn 2 pp\n" | sort -t " " -k3,3nr mm nn 2 pp aa bb 11 cc
Sort using multiple fields
You can also sort using multiple fields and also select numeric or reverse for each field as shown below:
$ printf "aa bb 11 cc\nmm nn 2 pp\nqq nn 2 pp\n" | sort -t " " -k3,3n -k1,1r qq nn 2 pp mm nn 2 pp aa bb 11 cc
Note that in case we specify multiple field using -k
, the order is also relevant and will have an impact on the outcome.