简体   繁体   中英

How to find minimum values from array

I've an array tab-delimited like this:

rs1000 0.09 red
rs1000 0.01 blue
rs2000 0.07 yellow
rs2000 0.08 black

and I'm trying to take the minimum values in the second column for rs in the first one.

I expect a result like this:

rs1000 0.01 blue
rs2000 0.07 Yellow

My file is too big to use Excel, so I need a linux script to reach my goal. Can you please suggest me a way to do that?

You can use sort twice:

sort -nk2,2 filename | sort -su -k1,1

The first invocation sorts the filename by the second column, the second one selects unique values from the column 1. The GNU sort selects the first line for each unique value, but I vaguely remember some other implementations behaving differently.

The quickest, but not the shortest, would be to use awk :

awk '(!($1 in m))||($2<m[$1]){m[$1]=$2; r[$1]=$0}END { for(i in r) print r[i] }' file

A posix compliant version with sort and awk would be:

$ sort -k1,1 -k2,2n file | awk '(f!=$1);{f=$1}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM