Locating the largest number and performing division

I have a tab delimited file with the following format

1 r 109 45 3 5 6 7
2 f  300 249 5 8 10
3 g 120 4 5 110 0
4 t 400 300 250 0 0
.....
.....
100,000 lines

I would like to get the largest number in columns 4, 5, 6, 7, 8 and divide that largest number with the number in column 3.

Desired output

1 r 109 45 3 5 6 7 0.41
2 f  300 249 5 8 10 0.83
3 g 120 4 5 110 0 0.9166
4 t 400 300 250 0 0 0.75

I tried excel today this but since it has over 100,000 lines, it is difficult to do it. Please let me know the best way to do it awk or see.

awk '{ for(N=3; N<=8; N++) if((A[N] == "") || (A[N]<$N)) A[N]=$N }
END {
        for(N=4; N<=8; N++) printf("%f / %f = %f\n", A[N], A[3], A[N]/A[3]);
}' inputfile

I guess this is what you want:

awk '{max=""; for (i=4; i<=NF; i++) if ($i>max) max=$i; print $0, max/$3}' file
1 r 109 45 3 5 6 7 0.412844
2 f  300 249 5 8 10 0.83
3 g 120 4 5 110 0 0.916667
4 t 400 300 250 0 0 0.75
1 Like

I wanted to print an output file as a tab delimited file. I tried the following:

 awk -F $ '\t' '{ print $1,$2,$3,$4+$5,$6,$7,$8,$9;}' inputfile > outputfile.txt

But the output is not tab delimited. Let me know

Read about the OFS variable in your AWK manual page.

Regards,
Alister