Posts Tagged csv

Sort according to the nth Column in Shell Script

You have a file that has multiple columns separated by a specific delimiter and you need to sort it according to a specific column which need not be the 1st column. If its not the 1st column then ordinary sort wont work. So we need to use the parameters of sort to specify the exact column so that it will sort according to that column only. There are parameters in sort like -t and -k, we use this for the sorting. So -t is to specify the delimiter and -k to specify the column needed. For example : We need to get the 3rd column of a csv file(csv file has , as delimiter) the option should be -t, -k3. We can use two more options for the same. -n for sorting the values as numbers instead of considering as text and -r for sorting in descending order ie biggest value on the top. So the final command is :

Continue reading “Sort according to the nth Column in Shell Script” »


, , , , ,

1 Comment