Sorting by two columns

Sorting by two columns

Post by Poke » Sat, 01 Mar 2003 07:40:10



I have a file in the following format:

Name    Score   Average Score

Matt    100
Fred    56      66
John    60              
Simon   56      55

I want to sort it as follows:

Name    Score   Average

Simon   56      55
Fred    56      66
John    60              
Matt    100

First the score is sorted and then the average scores are sorted
within the people with average scores.
Ive been trying to figure this out for a while, using awk and sort,
but with no luck.
Any help would be much appreciated!
Mant thanks,

Ed.

 
 
 

Sorting by two columns

Post by Barry Margoli » Sat, 01 Mar 2003 07:43:41




>I have a file in the following format:

>Name        Score   Average Score

>Matt        100
>Fred        56      66
>John        60              
>Simon       56      55

>I want to sort it as follows:

>Name        Score   Average

>Simon       56      55
>Fred        56      66
>John        60              
>Matt        100

>First the score is sorted and then the average scores are sorted
>within the people with average scores.
>Ive been trying to figure this out for a while, using awk and sort,
>but with no luck.

sort -k 2,2n -k 3,3n

This will handle the lines with the scores; you'll need to deal with the
column headings separately.

--

Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Sorting by two columns

Post by laura fairhe » Sat, 01 Mar 2003 07:49:33



Quote:>I have a file in the following format:

>Name        Score   Average Score

>Matt        100
>Fred        56      66
>John        60              
>Simon       56      55

>I want to sort it as follows:

>Name        Score   Average

>Simon       56      55
>Fred        56      66
>John        60              
>Matt        100

>First the score is sorted and then the average scores are sorted
>within the people with average scores.
>Ive been trying to figure this out for a while, using awk and sort,
>but with no luck.

Try looking in the man page for 'sort' - "man sort"

'sort' has the ability to sort by more than one key at a time
so you shouldn't need 'awk'.

Quote:>Any help would be much appreciated!
>Mant thanks,

>Ed.

seeyafrom
laura

--
alt.fan.madonna |news, interviews, discussion, writings
                |chat, exchange merchandise, meet fans....
                |Get into the groove baby you've got to... check us out!

 
 
 

Sorting by two columns

Post by William Par » Sat, 01 Mar 2003 10:23:07



> I have a file in the following format:

> Name    Score   Average Score

> Matt    100
> Fred    56      66
> John    60              
> Simon   56      55

> I want to sort it as follows:

> Name    Score   Average

> Simon   56      55
> Fred    56      66
> John    60              
> Matt    100

> First the score is sorted and then the average scores are sorted
> within the people with average scores.
> Ive been trying to figure this out for a while, using awk and sort,
> but with no luck.
> Any help would be much appreciated!
> Mant thanks,

man sort        (-k and -n options)
--

Linux solution for data management and processing.
 
 
 

1. Sorting By Second Column With Unique First Column

I need to sort data by the second column while keeping
the first column unique. There is probably an easy way to
do this that I'm totally missing.
    The first column is an inmate number. The second column
is the bond amount. I'm going to output the criminals based
on the bond amount, with the highest bonds at the top.
    My problem is that if someone has two bond amounts, that
person makes the list twice. I only one folks listed based on
the highest individual bond amount. (A guy with a $10k bond
should be before someone with three $5k bonds.)

Unsorted                          What I Want
98051461~250.00                   98051385~5000.00
98051461~0.00                     98051487~750.00
98051403~500.00                   98051403~500.00
98051487~750.00                   98051461~250.00
98051487~750.00
98051385~1500.00
98051385~1500.00
98051385~0.00
98051385~0.00
98051385~5000.00

    I'm using... sort -rnt "~" -k 2 $INPUT | uniq ...which
solves the problem when someone has multiple bonds of the
same amount but doesn't help when the bonds are varying.
    A perl solution would be a wonderful thing but I'm currently
getting the data externally (open DINNER, "sort -rnt \"~\" -k 2
$Temp | uniq |" or die "Can't fork: $!" ;) so I'll take any
solution that works. (Yes, the above is cheezy.)
    All suggestions (and requisite mocking) welcome.
    Many thanks.

    Matt

2. Moving passwords from HPUX?

3. How to have sort start sorting froma given column?

4. PPP telnet connection slow.

5. SORT command > sorting decimal numbers in columns on a file not working !

6. CDE font problems

7. how to join two files using two columns as key

8. Help with Virtual Terminals.

9. How to merge two columns of numbers from two different files?

10. sort by columns

11. sort -n with more than one column

12. Sorting a list by a date column

13. How to sort 2nd column in alphabetic