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!

max value

Status
Not open for further replies.

userand

Technical User
Apr 12, 2011
28
CH
Hello again,

sorry for this repeated post, but I need some help.

I have the following lines:

2 2 2 1 1
1 2 5 1 1
1 2 2
1 17 2 1 2 3 2 3

How can I print out the maximum value of a line?

I have tried this, but cannot helps me at the moment

awk '{for(i=1;i<=NF;i++){if($i > max) max = $i;}}END{print max}' < the/file/

but prints only 17 :(



 
The way your code is written, 17 is the correct answer. Your code is written to replace max only if a field is greater than max. 17 is numerically the largest field.

What do you expect it to print?
 
If you want to print out the maximum for each line, try

{max=$1;for(i=2;i<=NF;i++){if($i > max) max = $i}print max}

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top