How to sort files by multicolumn
Olivia Zamora
My file has 3 columns and I want to sort the data in the file by column 1 (DESC), column 2(ASC) and save the result to another file. How can I do that?
31 Answer
sort has the option to sort by multiple columns, do:
sort -k1,1 -k2,2 file.txt The above will sort the file first by whitespace separated column 1, then by column 2.
You can also set other delimiter than any whitespace by the -t option, e.g. setting , as the delimiter:
sort -t ',' -k1,1 -k2,2 file.txtCheck man sort.