The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2008
Registered User
 

Join Date: Jun 2008
Posts: 5
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-30-2008
Registered User
 

Join Date: Feb 2007
Posts: 11
Quote:
Originally Posted by clarcombe View Post
I am trying to sort a text file by two keys but the second key should be reversed. Does anyone have any suggestions?
You could sort the files on the first key and then with each segment of the sorted file containing lines where the first key is the same you must sort on the second key.

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};
  }
}
to convert this unsorted file
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
using this command
Code:
sort -t, -k4  unsorted.txt | sort5.pl
to this sorted file
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
Reply With Quote
  #3 (permalink)  
Old 06-30-2008
Registered User
 

Join Date: Jun 2008
Posts: 5
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
Reply With Quote
  #4 (permalink)  
Old 06-30-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 542
Show data

Show how the input and output looks like.
Reply With Quote
  #5 (permalink)  
Old 07-01-2008
Registered User
 

Join Date: Jun 2008
Posts: 5
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
Reply With Quote
  #6 (permalink)  
Old 07-01-2008
robotronic's Avatar
Can I play with madness?
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Code:
sort -t '|' -k4,4n -k5,5r input_file.txt
Reply With Quote
  #7 (permalink)  
Old 07-01-2008
Registered User
 

Join Date: Jun 2008
Posts: 5
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0