sort command help needed.

Hello,
suppose i have a student.dat file whose format is

studentno studentname semester marks1 marks2 marks3 total

then how can i sort first in semester wise and then total wise? I mean semester wise ranking wise record should be displayed. Semester should be displayed in ascending order (4 , 5. etc ) and result should be descending order(since it is to be displayed rank wise).

i tried with this but couln't succeed

 sort -n +2 -3 -r +6 -7 student.dat

Thanks in advance :slight_smile:

Hi,

Can you provide a sample file and desired output?

sure

input

s01 lula 6 75 65 55 195
s02 tutu 6 85 90 90 265
s03 laala 5 55 65 75 195
s04 butterkhor 6 70 70 70 210
s05 kuku 7 50 50 40 140

output

s03 laala 5 55 65 75 195
s02 tutu 6 85 90 90 265
s04 butterkhor 6 70 70 70 210
s01 lula 6 75 65 55 195
s05 kuku 7 50 50 40 140

Try this:

sort -k3,3 -k7,7r input.file

thanks a lot ripat :slight_smile:

put your file content into a array, then use below user-defined sort logic to sort your array, then you may get what you want. :slight_smile:

sub lsort{
	my @t1=split(" ",$_[0]);
	my @t2=split(" ",$_[1]);
	if($t1[2]<$t2[2]){
		return -1;
	}
	elsif($t1[2]>$t2[2]){
		return 1;
	}
	else{
		if($t1[6]<$t2[6]){return 1;}
		elsif($t1[6]>$t2[6]){return -1;}
		else{ return 0;}
	}
}

sort -k3,3 -k7,7r input.file --- this gives

s03 laala 5 55 65 75 195
s02 tutu 6 85 90 90 265
s04 butterkhor 6 70 70 70 210
s01 lula 6 75 65 55 195

Is this sorted???