awk print line with highest value

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk print line with highest value
# 1  
Old 04-27-2018
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

Code:
log text text R59FJ
log text text R63FT
log text text R60JX
log1 text text R63EA
log1 text text R60JX

desired output
Code:
log text text R63FT
log1 text text R63EA

tried this but not getting the right output. Any help would be appreciated.
Code:
awk '!a[$4]++ && l {print l} {l=$0}; END {print l}'

# 2  
Old 04-27-2018
Code:
awk '$4 > a[$1] {a[$1]=$4; b[$1]=$0}; END {for (i in b) print b[i]}' infile

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 04-28-2018
Although not mentioning it, neither in the specification nor in the sample code, your desired output implies you want the "maximum" per $1 value.
Are you talking of $4's entire string value, or of the numerical part only? What if it starts with an "S" or an "s"?
If you go for the numerical part, try
Code:
awk '{T = $4; gsub (/[^0-9]/, "", T); if (T > a[$1]) {a[$1]=T; b[$1]=$0}}; END {for (i in b) print b[i]}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

4. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 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

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

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

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

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

10. 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
Login or Register to Ask a Question