![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sort on multiple keys | teqmem | UNIX for Advanced & Expert Users | 1 | 04-12-2007 10:15 PM |
| Hot Keys | cwtlr | Shell Programming and Scripting | 8 | 12-14-2006 09:07 AM |
| Hot Keys | cwtlr | UNIX for Dummies Questions & Answers | 2 | 12-01-2006 09:50 AM |
| SSH Keys Help | sysera | UNIX for Advanced & Expert Users | 1 | 08-09-2004 11:57 PM |
| arrow keys / special keys | raguramtgr | UNIX for Dummies Questions & Answers | 3 | 02-19-2004 10:45 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Sort on two keys
Hello,
I am trying to sort a text file by two keys but the second key should be reversed. I have tried -nt '|' -k 4 -rk 5 but it just sorts reversed on key 4. Does anyone have any suggestions ? Thanks |
| Forum Sponsor | ||
|
|
|
|||
|
Quote:
One long-winded way of doing that would be to use the perl script: Code:
#! /usr/bin/perl -wnl
($undef, $undef, $undef, $primary_key, $secondary_key, $undef) = split /,/;
if (!defined($previous_key)) {
$previous_key = $primary_key;
}
else {
if ($previous_key ne $primary_key) {
for $key (sort {$b cmp $a} keys %segment) {
print $segment{$key};
}
%segment = ();
}
}
if (!defined($segment{$secondary_key})) {
$segment{$secondary_key} = $_;
}
else {
$segment{$secondary_key} .= "\n" . $_;
}
$previous_key = $primary_key;
END {
for $key (sort {$b cmp $a} keys %segment) {
print $segment{$key};
}
}
Code:
bbb,ccc,ddd,eee,aaa,fff ddd,eee,fff,aaa,ccc,bbb bbb,ccc,ddd,eee,fff,aaa bbb,ccc,ddd,eee,aaa,fff fff,aaa,bbb,ccc,ddd,eee eee,fff,aaa,bbb,ccc,ddd aaa,bbb,ccc,ddd,eee,fff ccc,ddd,eee,fff,aaa,bbb ddd,eee,fff,aaa,bbb,ccc ccc,ddd,eee,fff,aaa,bbb Code:
sort -t, -k4 unsorted.txt | sort5.pl Code:
ddd,eee,fff,aaa,ccc,bbb ddd,eee,fff,aaa,bbb,ccc eee,fff,aaa,bbb,ccc,ddd fff,aaa,bbb,ccc,ddd,eee aaa,bbb,ccc,ddd,eee,fff bbb,ccc,ddd,eee,fff,aaa bbb,ccc,ddd,eee,aaa,fff bbb,ccc,ddd,eee,aaa,fff ccc,ddd,eee,fff,aaa,bbb ccc,ddd,eee,fff,aaa,bbb Last edited by risby; 06-30-2008 at 07:29 AM. Reason: forgot to print the final, saved, segment |
|
|||
|
Thanks for the reply.
Unfortunately I am calling unix from within another program (Datastage). I really wanted to keep it to one unix command but if this is not possible I will have to work around it. One way I was going to do this was to change the generation of the second key so that it is always in the right order so I only have to use one key. Thanks anyway |
|
|||
|
BPC_TRANS_CHARG|000009|000|278|TGL0009|S1_C12 J60867SNCF0R n'est pas present dans PS_S1_C12_VW
BPC_TRANS_CHARG|000009|000|278|EGL0009|S1_C12 J60867SNCF0R n'est pas present dans PS_S1_C12_VW BPC_TRANS_CHARG|000009|000|86|TGL0009|S1_C12 J60867SNCF0R n'est pas present dans PS_S1_C12_VW BPC_TRANS_CHARG|000009|000|86|EGL0009|S1_C12 J60867SNCF0R n'est pas present dans PS_S1_C12_VW Field 4 e.g 278 has to be ascending and field 5 descending TGL0009 |
|
|||
|
Excellent. That works perfectly
What does the -k4,4n -k5,5r mean. I understand the 4n part but what does the , do? I modified the command sort -t '|' -k4n -k5r BPC_TRANS_CHARG000009_000_Anomalie.csv and this works as well Thanks. Colin |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|