AWK or KSH : Sort, Group and extract from 3 files

Hi,
I've the following two CSV files:

File1.csv                                      File2.csv
 
Class,Student#                             Student#,Marks
1001,6001                                   6002,50
1001,6002                                   6001,60
1002,7000                                   6003,20
1002,7001                                   8000,10
1002,7002                                   7000,30
1002,7003                                   7002,90
1003,8000                                   7001,50
                                            7003,20

Now, I need to extract the Students who got max marks in each Class and store it in File3.csv. For example, File3.csv should contain Class,Student# who got max marks, Marks.

Also, if there are more than 3 students in a class, then the second highest mark should be taken.

So, the final output should be

File3.csv:
  
Class,Student#,Marks
1001,6001,60    --> Max mark
1002,7001,50    --> Second highest
1003,8000,10

I am using AIX 5.3 version. The file sizes are huge. Please suggest on how to achieve this in either awk or ksh or sed. Thanks.

This looks suspiciously like a homework which is against the rules.
Please show your own effort and indicate where you're having difficulties.

The following code gives the max marks... I need to know on how to get second highest...


sort -t',' -k2 File1.csv -o File1.csv

sort -t',' -k1 File2.csv |\
join -t',' -e 'ZZZZ' -1 2 -2 1 -o 1.1 2.2 1.2 File1.csv - | sort |\
awk -F',' 'BEGIN {g=$1;v=$3} {if($1!=g){print g,v;} g=$1;v=$3} END {print g,v}'

The code gives me max marks and its corresponding student#. But I am not sure on how to get the second highest mark, if there are more than 3 students in a class...


sort -t',' -k2 File1.csv -o File1.csv

sort -t',' -k1 File2.csv |\
join -t',' -e 'ZZZZ' -1 2 -2 1 -o 1.1 2.2 1.2 File1.csv - | sort |\
awk -F',' 'BEGIN {g=$1;v=$3} {if($1!=g){print g,v;} g=$1;v=$3} END {print g,v}'