How to match two fields

Hello Every one,

I am in need of all ur's help.

Let me straight explain the problem ..

I have a file "player" which contains three players with their NAME , RUN and ECONOMY rate.

----------
Name Run Economy
David 23 8
stuart 12 12
string 33 4
-----------

Now i want to find best player who has highest run but lowest economy rate.

Means according to this file best player shud be "string".

I did try writing script but that does not work when i change the data .

What is did is :

first i took highest run and lowest economy in a variables (i and j).

i=`cat player | awk 'BEGIN { FS= " " } {print $2}' | grep -v Run | sort -nr | head -1`

---------
bash-3.1$ echo $i
33
------

j=`cat player | awk 'BEGIN { FS= " " } {print $3}' | grep -v Economy | sort -n | head -1`

---------
bash-3.1$ echo $j
3
bash-3.1$
----------

Now the problem is how to match two fields that am not sure.

Plzzz help me .. :confused: ( i tried fgrep but it does not work ).

Please help me out with this.

Thanks,

And what to do if someone has the highest run and someone else the lowest economy?

Regards

Dear Franklin52,

Thank you for answering me.

Can we write a script if we do not consider that ? If yes please let me know.

Thanks,

To find the player with the highest run you can do something like:

awk '
max<$2{max=$2;s=$0}
END{print "Max = " s}' player

Now it's your task to find the player with the lowest economy rate.

Regards

Dear Franklin52,

Yea sure i will give it a try. :slight_smile:

Thank u very much for ur reply.

Thanks,
:slight_smile:

What about this solution?

awk 'max<$2-$3{max=$2-$3;s=$0}END{print s}'

----------
awk 'max<$2-$3{max=$2-$3;s=$0}END{print s}'
-----------

Hello danmero,

Yes , it works great. :slight_smile:

Could u plz share the logic with me ?

Thanks,

Base on your original post, you should find the most efficient player where RUN is the most important factor.

awk '
max<$2-$3      # Compare recordset efficiency score($2-$3) to max(higher efficiency score) value.
{
max=$2-$3      # If recordset has efficiency score greater that max, assign recordset efficiency score to max.
s=$0           # and assign the recordset to s variable.
}              # Loop until end of file.
END
{print s}      # At the end print the value of s, the recordset having the highest efficiency value.
'  data.file

I don't know but this questions look like a homework to me(check the forum rules).