Only print the entries with the highest number?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Only print the entries with the highest number?
# 1  
Old 02-21-2012
Only print the entries with the highest number?

Just want to say this is great resources for all thing Unix!!
Code:
cat tmp.txt
A 3
C 19
A 2
B 5
A 1
A 0
C 13
B 9
C 1

Desired output:
Code:
A 3
B 9
C 19

The following work but I am wondering if there is a better way to do it:
Looking for better performance on larger and more complex file.
Code:
awk '{print $1}' tmp.txt | sort | uniq > grep.lst
sort -k1,1 -k2,2nr tmp.txt > tmp.sort.txt
for i in `cat grep.lst`
do
 grep $i tmp.sort.txt | head -1 >> good.out
done
cat good.out

A 3
B 9
C 19

Thanks in advance.

David

Last edited by fpmurphy; 02-21-2012 at 06:38 PM.. Reason: code tags please!
# 2  
Old 02-21-2012
Code:
awk '$2>M[$1]{M[$1]=$2}END{for (i in M) print i,M[i]}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-21-2012
MySQL

Thanks!! That work great.
Code:
awk '$2>M[$1]{M[$1]=$2}END{for (i in M) print i,M[i]}' file

I will go RTFM on Awk Smilie but would love to be taught how to fish instead of given say fish Smilie

Put the 2nd column Data into Hash-Array? Since it's Using Column 1 as index?
Code:
{M[$1]=$2}

This part I think I understand:
For each element print the index(which is data for column 1) then the value of hash-array:
Code:
for (i in M) print i,M[i])

I am missing the part how awk put the highest value in the the array?

Thanks
David

Last edited by Franklin52; 02-22-2012 at 06:04 AM.. Reason: Code tags
# 4  
Old 02-21-2012
Code:
sort -k1,1 -k2,2nr infile | awk '!A[$1]++'

# 5  
Old 02-22-2012
$2>M[$1] - This part is testing if current value in column two is bigger than maximum value saved so far. If it is, then the value is replaced with the code that you described.
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

Sort from highest to lowest number

Hi Guys, I am looking for a way to sort the output below from the "Inuse" count from Highest to Lowest. Is it possible? Thanks in advance. user1 0.12 0.06 0 0.12 User Inuse Pin Pgsp Virtual Unit:... (4 Replies)
Discussion started by: jaapar
4 Replies

5. Shell Programming and Scripting

Print the key with highest value

print the key with highest value input a 10 a 20 a 30 b 2 b 3 b 1 output a 30 b 3 (9 Replies)
Discussion started by: quincyjones
9 Replies

6. Shell Programming and Scripting

Find highest number - working but need help!

Hello all, I am new to this and need some help or maybe steer me to the right direction! I wrote a script to get the highest number and prints it on the screen, the script basically asks the user to input numbers, and then prints the highest number! very simple it works like this $sh max.sh... (8 Replies)
Discussion started by: unknownsolo
8 Replies

7. 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

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... (1 Reply)
Discussion started by: dliving3
1 Replies

9. Shell Programming and Scripting

Extract the highest number out

Hi Gurus, I've using HPUX B.11.23 U ia64 with shell = sh. I've been having some problem get the highest number of this script. Actually I wanted to get the highest number from this listing (TEST123 data and based on this highest number, there will be email being sent out. For example,... (6 Replies)
Discussion started by: superHonda123
6 Replies

10. Shell Programming and Scripting

find the highest number in the file

Hi, I have a file a.txt and it has values in it Eg :- I need to read through the file and find the number that is the greatest in them all. Can any one assit me on this. Thanks (30 Replies)
Discussion started by: systemali
30 Replies
Login or Register to Ask a Question