Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix Awk

Status
Not open for further replies.

PoxKing

Technical User
May 3, 2010
4
GB
There is a file that contains:

MFT 90
FFF 87
SIA 76
SSS 94

This file can be called Data.txt

I'd like to know how to use awk to compare the second fields and return the line with the biggest number.

Many thanks.
 
awk '$2>m{m=$2;x=$0}END{print x}' Data.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks a lot even you did it wrong :p
I added if and it worked.
 
you did it wrong
Really ?
I added if
Where ?
 
It was not totally really wrong, but needed 'if'

awk {if ($2>m)
{m=$2;x=$0}
END{print x}' Data.txt

the previous command didn't work.
 
I highly doubt that your posted code don't raise an error ...
 
awk {if ($2>m)
{m=$2;x=$0}}
END{print x}' Data.txt
it works, tested.
 
it works, tested
Again, not as posted.

It's why I believe you didn't test my exact code.
 
No if is required because there is an implicit if before every expression { code block } pair.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top