Sort a file but keep row pairs together

sort

I have a file with entries like the following:

female,9,13,6.3152956461
female,12,12,5.4797699786
female,11,11,3.6421699174
female,9,14,4.5933365997
female,8,14,2.4181574607

etc.

I first want to remove all but the first 2 columns, then separate them by a space instead of a comma. Ultimately though my problem is that I need to sort by column 1, then column 2 but I have to keep the pairing in a given row intact.

I believe that I need to use sed to switch the column deliminator and sort to do the sorting but I can't figure out how to keep the row pairings together between the 2 sorts.

Best Answer

You mean like this?

$ sort -t, -k1,1 -k2,2n file
female,8,14,2.4181574607
female,9,13,6.3152956461
female,9,14,4.5933365997
female,11,11,3.6421699174
female,12,12,5.4797699786

This will sort ,-delimited data on the first field alphabetically, then on the second field numerically. The sorting will first sort using the first field, and for records that have identical first fields, the second field will be used for sorting.


To remove the last few columns afterwards (or rather, keep columns 1 and 2):

$ sort -t, -k1,1 -k2,2n file | cut -d, -f1,2
female,8
female,9
female,9
female,11
female,12

To remove that comma too:

$ sort -t, -k1,1 -k2,2n file | cut -d, -f1,2 | tr ',' ' '
female 8
female 9
female 9
female 11
female 12

But this is not needed for the sorting, obviously, as you may tell sort what delimiter to use.