How to Sort Files by Multicolumn
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? 3 1...
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?
1 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.txt
Check man sort.