Print line with highest value from one column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print line with highest value from one column
# 1  
Old 06-28-2011
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 link to (only 4 more posts until I can spam!!Smilie).

My problem is that I have more columns, and want the whole line printed. For the tab delimited example:

LineIDSortExtraExtra2
1A343
2A121
3A232
4A111
5B111
6B121
7B333
8C111
9D313
10E313
11E323
I want to print one line for every unique ID value that has the highest value in Sort. Giving the output:

LineIDSortExtraExtra2
1A343
7B333
8C111
9D313
11E323

If the sort value is equal for any given ID, I don't really care which is returned as in ID=E.

Franklin52's answer (modified for my data) in the previous post mentioned was close:
Code:
 awk 'a[$2] < $3{a[$2]=$3} END{for(i in a){print i, a[i]}}' file

However, I can't seem to get it to print the whole line. Please let me know what I am missing!

Thanks in advance!
# 2  
Old 06-28-2011
i know, this is the worst sequence of commands to get output Smilie

Code:
 
$for i in `nawk '{print $2}' test | uniq`; do grep $i test | sort -nrk3 | head -1; done
1 A 3 4 3 
7 B 3 3 3 
8 C 1 1 1 
9 D 3 1 3 
11 E 3 2 3

we can easily do it awk.. but i didnt get idea in awk.
This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 06-28-2011
Thanks itkamaraj!

I had managed to twist the close Franklin52 answer into:
Code:
 awk 'BEGIN{FS="\t"};a[$2] < $3{a[$2]=$3 "\t" $0} END {for(i in  a){print a[i]}}' TEST.TXT | awk '{$1="";$0=substr($0,2)} {print}' | sed  's/ /'$'\t/g' |sort -n

which seems to get the job done, but it was 3AM when I pieced this together and decided sleep was important.

Although both seem to work, I used your solution, primarily because I THINK a can understand what it is doing (unlike my solution).

So you are printing the uniq values in the column of interest and making them an array. You then search (grep) my file for lines containing each value in the array. Sort each value in reverse numerical order in the column I want the highest value in (k3). Then you print the first line (highest column 3 value) for each individual in the array. Then you are done.

Does that sound like a correct explanation? Just trying to understand what I am doing, instead of just doing what I am doing! Regardless, it worked perfectly! Thanks again.
# 4  
Old 06-29-2011
Hi dliving3,
actually,you dont need to run awk with two times.Smilie
try to add extra condion states and add the full lines to b[] array.
for example
Code:
# awk '(NR==1);a[$2]<$3||d[$2]<$4{a[$2]=$3;d[$2]=$4;b[$2]=$0};END{for(i in b)if(b[i] !~ /ID/){print b[i]}}' TEST.TXT
Line    ID      Sort    Extra   Extra2
1       A       3       4       3
7       B       3       3       3
8       C       1       1       1
9       D       3       1       3
11      E       3       2       3

regards
ygemici
 
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

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

5. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

Find lines with matching column 1 value, retain only the one with highest value in column 2

I have a file like: I would like to find lines lines with duplicate values in column 1, and retain only one based on two conditions: 1) keep line with highest value in column 3, 2) if column 3 values are equal, retain the line with the highest value in column 4. Desired output: I was able to... (3 Replies)
Discussion started by: pathunkathunk
3 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... (1 Reply)
Discussion started by: dliving3
1 Replies

9. Shell Programming and Scripting

read file line by line print column wise

I have a .csv file which is seperated with (;) inputfile --------- ZZZZ;AAAA;BBB;CCCC;DDD;EEE; YYYY;BBBB;CCC;DDDD;EEE;FFF; ... ... reading file line by line till end of file. while reading each line output format should be . i need to print only specific columns let say 5th... (2 Replies)
Discussion started by: rocking77
2 Replies

10. Shell Programming and Scripting

How to print last column of line

Hello folks, Please guide me i have a file file.txt that have below text. PETER JOHN peter@example.com John Col john@example.com Sara Paul sara@example.com I just want to extract only email address list. (5 Replies)
Discussion started by: learnbash
5 Replies
Login or Register to Ask a Question