Sort on two keys


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Sort on two keys
# 1  
Old 06-30-2008
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
# 2  
Old 06-30-2008
Quote:
Originally Posted by clarcombe
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 11:29 AM.. Reason: forgot to print the final, saved, segment
# 3  
Old 06-30-2008
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.Smilie

Thanks anyway
# 4  
Old 07-01-2008
Show data

Show how the input and output looks like.
# 5  
Old 07-01-2008
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
# 6  
Old 07-01-2008
Code:
sort -t '|' -k4,4n -k5,5r input_file.txt

# 7  
Old 07-01-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sort with multiple keys

Please suggest a sort command to achieve the below task. Thanks. I want to sort a file considering multiple keys. Sort Keys: Field 2, Field4 and Field6 Input file vqrs,16,zzz,1235,eq,T abcd,11,zzz,1234,pq,F abcd,10,zzz,1235,pq,F lqrs,15,zzz,1235,eq,T pqrs,12,zzz,1234,eq,F... (3 Replies)
Discussion started by: pretty1234
3 Replies

2. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

4. Shell Programming and Scripting

Alternate to sort --random-sort

sort --random-sort The full command is path=`find /testdir -maxdepth 1 -mindepth 1 -type d | ***Some sort of sort function*** | head -1` I have a list I want to randomly sort. It works fine in ubuntu but on a 'osx lion' sort dosen't have the --random-sort option. I don't want to... (5 Replies)
Discussion started by: digitalviking
5 Replies

5. Shell Programming and Scripting

[SOLVED] Sort on multiple keys

Can you guys pls take a look at this. I need to sort this list of numbers as follows: 2nd col first, then 1st col, then 3rd col, all in reverse (highest to lowest). I'm doing this: sort -k 2,2nr -k 1,1nr -k 3,3gr but, as you see, the 3rd col does not get sorted properly. Any idea... (0 Replies)
Discussion started by: mamboknave
0 Replies

6. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

7. Shell Programming and Scripting

What are public keys in ssh and how do we create the public keys??

Hi All, I am having knowledge on some basics of ssh and wanted to know what are the public keys and how can we create and implement it in connecting server. Please provide the information for the above, it would be helpful for me. Thanks, Ravindra (1 Reply)
Discussion started by: ravi3cha
1 Replies

8. Shell Programming and Scripting

How to Sort Floating Numbers Using the Sort Command?

Hi to all. I'm trying to sort this with the Unix command sort. user1:12345678:3.5:2.5:8:1:2:3 user2:12345679:4.5:3.5:8:1:3:2 user3:12345687:5.5:2.5:6:1:3:2 user4:12345670:5.5:2.5:5:3:2:1 user5:12345671:2.5:5.5:7:2:3:1 I need to get this: user3:12345687:5.5:2.5:6:1:3:2... (7 Replies)
Discussion started by: daniel.gbaena
7 Replies

9. UNIX for Advanced & Expert Users

sort on multiple keys

Hello, Say I have a file with plain text as shown below. Some columns may have multiple words (like "DESC 1", "DESC 1 2", "DESC 1 2 3"). Let's say the file below has 4 columns: 1st(AA), 2nd(BB), 3rd(DESC 1, ...), 4th(CC 1, ...). 1234567890123456789012345678901234567890 AA BB DESC 1... (1 Reply)
Discussion started by: teqmem
1 Replies

10. UNIX for Dummies Questions & Answers

arrow keys / special keys

how to use the arrow keys in shell scripting. is there any special synatax / command for this. i just want to use the arrow keys for navigation. replies appreciated raguram R (3 Replies)
Discussion started by: raguramtgr
3 Replies
Login or Register to Ask a Question