2013-05-16 115 views
1

輸入文件排序CSV數據按多個列

1,A,SDF,24,55,55 
2,T,AAS,22,12,32 
3,TR,SS,14,19,69 
4,DD,DD,14,59,39 
5,SW,DD,14,39,59 
6,SB,DS,12,26,126 
7,QQ,SWD,12,19,199 
8,QA,DD,12,17,167 
9,AA,QQ,10,9,49 

所需的輸出

1,A,SDF,24,55,55 
2,T,AAS,22,12,32 
3,TR,SS,14,19,69 
5,SW,DD,14,39,59 
4,DD,DD,14,59,39 
7,QQ,SWD,12,19,199 
8,QA,DD,12,17,167 
6,SB,DS,12,26,126 
9,AA,QQ,10,9,49 

邏輯:第一排序第四列反轉,然後排序第六列

反向

我試過以下,但沒有工作

sort -t ',' -nrk 4,6 -s Input_file 

sort -t ',' -k 4,4n -k 6,6n Input_file 
+0

你要排序的第二次或分選後第4次與第6列? – speakr

+0

排序第4列後4 .. – Debaditya

+0

http://theunixshell.blogspot.com/2012/12/sort-based-on-column-and-characters.html – Vijay

回答

4

我得到這個:

kent$ sort -nrt, -k4,4 -k6,6 file                                   
1,A,SDF,24,55,55 
2,T,AAS,22,12,32 
3,TR,SS,14,19,69 
5,SW,DD,14,39,59 
4,DD,DD,14,59,39 
7,QQ,SWD,12,19,199 
8,QA,DD,12,17,167 
6,SB,DS,12,26,126 
9,AA,QQ,10,9,49 
+0

謝謝..... :) – Debaditya

3

你可能有興趣在一個Perl的解決方案:

print join(',', @$_), "\n" for 
sort { $b->[3] <=> $a->[3] or $b->[5] <=> $a->[5] } 
map { chomp; [ split /,/ ] } <>; 
+0

謝謝... ... :) – Debaditya