1753943 Members
8461 Online
108811 Solutions
New Discussion юеВ

sort command question

 
SOLVED
Go to solution
kenny chia
Regular Advisor

sort command question

I need to sort a csv file by the 3rd column then by the 2nd column

I used the following command

sort -t, -k 3,2 data.csv

but it ends up sorting by column 2 then by column 3 only

how should I sort by column 3 then column 2??

All Your Bases Are Belong To Us!
4 REPLIES 4
Steve Steel
Honored Contributor

Re: sort command question

Hi


Try

sort -t, -k 3,3 -k 2,2 data.csv



steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Franky_1
Respected Contributor

Re: sort command question

Hi,

cat | sort -k 3 -k 2 >

should do the trick

Regards

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor
Solution

Re: sort command question

We can not use key values as,

-k 3,2

start point of key = 3
end point ok key = 2

Man page -->
The characters at positions
field_start and field_end are included in the key
field, providing that field_end does not precede
field_start.

IF you want to sort based on column 3 first then column 2 next then,

use two keys as,

-k 3 -k 2 ( -k 3,3 -k 2,2 )

it will process sorting with 3rd field then 2nd field.

Examples:
Master1 # cat /tmp/for.log
hai,23,4
bye,56,1
you,67,2
me,34,6

Master1 # sort -t , -k 3 -k 2 /tmp/for.log
bye,56,1
you,67,2
hai,23,4
me,34,6

Master1 # sort -t , -k 3,2 /tmp/for.log
bye,56,1
hai,23,4
me,34,6
you,67,2

Regards
Muthu

Easy to suggest when don't know about the problem!
kenny chia
Regular Advisor

Re: sort command question

Hi Muthu

sort -t, -k 3,3 -k 2,2

works
All Your Bases Are Belong To Us!