Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash output the line with highest value

my question is pretty much like this one but with one difference; i want the output the line that has highest score on the 3rd tab. my data is like:

1.gui  Qxx  16
2.gui  Qxy  23
3.guT  QWS  11

and i want to get this:

1.gui  Qxy  23
3.guT  QWS  11

I used:

cat file.f | uniq | cut -d" " -f3 | sort | uniq -d >>out.f

but did not get what i want!?

like image 448
teutara Avatar asked Nov 27 '12 09:11

teutara


1 Answers

With sort:

$ sort -rk3 file             # Sort on column 3, display all results

2.gui  Qxy  23
1.gui  Qxx  16
3.guT  QWS  11

$ sort -rk3 file | head -2   # Sort on column 3, filter number of results

2.gui  Qxy  23
1.gui  Qxx  16

$ sort -rk3 file | uniq      # Sort on column 3, on display unique results 

2.gui  Qxy  23
1.gui  Qxx  16
3.guT  QWS  11

-r reverse sort, highest first.

-k3 sort on the 3rd column.


If you only want to display line which the 3rd column is greater than some value (i.e. 15) then try this using awk:

awk '$3>15' file | sort -rk3  # Display line where column 3 > 15 and sort

2.gui  Qxy  23
1.gui  Qxx  16
like image 169
Chris Seymour Avatar answered Sep 28 '22 17:09

Chris Seymour