Print the key with highest value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print the key with highest value
# 1  
Old 03-13-2012
Print the key with highest value

print the key with highest value

input
Code:
a       10
a       20
a       30
b       2
b       3
b       1

output
Code:
a       30
b       3

# 2  
Old 03-13-2012
Code:
awk '{a[$1]=a[$1]<$2?$2:a[$1]} END {for(i in a) print i,a[i]}'

This User Gave Thanks to complex.invoke For This Post:
# 3  
Old 03-13-2012
Thanx. Can you also modify it so that it can print the max if the value is positive and minimum if the value is negative ?

Fr ex:
Code:
a -1
a -2
a -3
b 1
b 2
b 3

Code:
a -3
b 3

# 4  
Old 03-13-2012
Code:
awk '{if($2<0)a[$1]=a[$1]>$2?$2:a[$1];else a[$1]=a[$1]<$2?$2:a[$1]} END {for(i in a) print i,a[i]}'

or

Code:
sort -rg infile | awk '!a[$1]++'

# 5  
Old 03-13-2012
Assuming that for a $1 value, all $2 value have the same sign :

Code:
awk '{a[$1]=sqrt($2^2)>sqrt(a[$1]^2)?$2:a[$1]}END{for(i in a) print i,a[i]}' infile

# 6  
Old 03-13-2012
So then is -4 higher than 3?
Code:
awk '$2*$2>A[$1]*A[$1]{A[$1]=$2} END{for(i in A) print i,A[i]}' infile


@huaihuaiz3: that second solution will probably not work reliably. Just try adding an extra space between field 1 and 2 on one of the lines and see what happens to the sort..
# 7  
Old 03-14-2012
Is it possible to sum the values instead of taking higher value ?

Code:
a -1
a -2
a -3
b 1
b 2
b 3

Code:
a -6
b 6

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk print line with highest value

grepping on a value but then want to print only those lines that have the highest value in the 4th column log text text R59FJ log text text R63FT log text text R60JX log1 text text R63EA log1 text text R60JX desired output log text text R63FT log1 text text R63EAtried this but not getting... (2 Replies)
Discussion started by: jimmyf
2 Replies

2. Shell Programming and Scripting

Print whole line with highest value from one column

Hi, I have a little issue right now. I have a file with 4 columns test0000002,10030010330,c_,218 test0000002,10030010330,d_,202 test0000002,10030010330,b_,193 test0000002,10030010020,c_,178 test0000002,10030010020,b_,170 test0000002,10030010330,a_,166 test0000002,10030010020,a_,151... (3 Replies)
Discussion started by: Ebk
3 Replies

3. AIX

Print whole line with highest value from one column

Hi, I have a little issue right now. I have a file with 4 columns test0000002,10030010330,c_,218 test0000002,10030010330,d_,202 test0000002,10030010330,b_,193 test0000002,10030010020,c_,178 test0000002,10030010020,b_,170 test0000002,10030010330,a_,166 test0000002,10030010020,a_,151... (2 Replies)
Discussion started by: Ebk
2 Replies

4. Shell Programming and Scripting

HHow to print the group with a highest value within a set

How to print the names with a highest value within a set and filter if it is the only unique group within the same set input sets names value groups j007 shot1 0.6 a j007 shot2 0.5 b j007 shot3 0.4 bb j007 shot4 0.3 bc j007 shot5 0.2 ... (8 Replies)
Discussion started by: quincyjones
8 Replies

5. Shell Programming and Scripting

Need to print duplicate row along with highest version of original

There are some duplicate field on description column .I want to print duplicate row along with highest version of number and corresponding description column. file1.txt number Description === ============ 34567 nl21a00is-centerdb001:ncdbareq:Error in loading init 34577 ... (7 Replies)
Discussion started by: vijay_rajni
7 Replies

6. Shell Programming and Scripting

Only print the entries with the highest number?

Just want to say this is great resources for all thing Unix!! cat tmp.txt A 3 C 19 A 2 B 5 A 1 A 0 C 13 B 9 C 1 Desired output: A 3 B 9 C 19 The following work but I am wondering if there is a better way to do it: (4 Replies)
Discussion started by: chirish
4 Replies

7. UNIX for Advanced & Expert Users

Print line based on highest value of col (B) and repetion of values in col (A)

Hello everyone, I am writing a script to process data from the ATP world tour. I have a file which contains: t=540 y=2011 r=1 p=N409 t=540 y=2011 r=2 p=N409 t=540 y=2011 r=3 p=N409 t=540 y=2011 r=4 p=N409 t=520 y=2011 r=1 p=N409 t=520 y=2011 r=2 p=N409 t=520 y=2011 r=3 p=N409 The... (4 Replies)
Discussion started by: imahmoud
4 Replies

8. UNIX for Dummies Questions & Answers

Print line with highest value from one column

Hi everyone, This is my first post, but I have already received a lot of help from the forums in the past. Thanks! I've searched the forums and my question is very similar to an earlier post entitled "Printing highest value from one column", which I am apparently not yet allowed to post a... (3 Replies)
Discussion started by: dliving3
3 Replies

9. UNIX for Dummies Questions & Answers

Print line with highest value from one column

Hi everyone, This is my first post, but I have already received a lot of help from the forums in the past. Thanks! I've searched the forums and my question is very similar to an earlier post entitled "Printing highest value from one column", which I am apparently not yet allowed to post a... (1 Reply)
Discussion started by: dliving3
1 Replies

10. Shell Programming and Scripting

Perl ? - How to find and print the lowest and highest numbers punched in by the user?

. . . . . . (3 Replies)
Discussion started by: some124one
3 Replies
Login or Register to Ask a Question