View Full Version : sorting???
kiss2rvp
05-26-2002, 06:34 PM
Hi.....i was just wondering how do i use the sort command to sort the following format from highest to lowest with the number the key but still keep the fields belonging to that key with it in the final output???
format i have now:
2
A
B
C
1
a
r
g
3
h
r
t
and final output should be :
3
h
r
t
2
A
B
C
1
a
r
g
thanks in advance.....
Lynux-Penguin
05-26-2002, 10:46 PM
you need a pattern first otherwise you will just end up manually placing the chars where they belong in the array
stautze
05-27-2002, 08:15 PM
if every number has three letters after it then you can use a really simple structure like this:
typedef struct entry {
int key;
char letter1;
char letter2;
char letter3;
} Entry;
Better though, would be this where the key could have any number of letters that are associated with it:
typedef struct entry {
int key;
char *letters;
} Entry;
Hi!
First, I would replicate the key in front of each line (using awk, for example).
Then do a stable sort on this key (treating it as a number, and reversing the sort order)
Finally, I would kill the superfluous key (again using awk).
cat unsortedfile | awk '{if($1==$1+0) key=$1; print key, $1}' | sort -s -k 1nr | awk '{print $2}' > sortedfile
Have fun...
alex
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.