Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get minimum value rows

Status
Not open for further replies.

opert

Technical User
May 4, 2011
6
Hi all,

I have the following data rows:

Code:
4       1       0.0518504806582944           0.0      7.3
4       2       0.0518504806582944          30.0      7.9
4       3       0.0518504806582944          60.0      8.0 
4       1       4.2634994446132923           0.0      2.8       
4       2       4.2634994446132923          30.0      7.9       
4       3       4.2634994446132923          60.0      8.0       
135     2       0.3377154644077501          30.0      0.9       
135     3       0.3377154644077501          60.0      1.5        
135     5       0.3377154644077501         120.0      2.8       
135     6       0.3377154644077501         150.0      4.7       
135     2       26.4678875783977468         30.0      0.9        
135     3       26.4678875783977468         60.0     12.0        
135     4       26.4678875783977468         90.0      1.7        
135     5       26.4678875783977468        120.0      2.8       
135     6       26.4678875783977468        150.0      4.7

Now, I would like to get those rows, in which the value of the 3rd column is minimal, i.e. in the example above I should get these rows as a result

Code:
4       1       0.0518504806582944           0.0      7.3
4       2       0.0518504806582944          30.0      7.9
4       3       0.0518504806582944          60.0      8.0      
135     2       0.3377154644077501          30.0      0.9       
135     3       0.3377154644077501          60.0      1.5        
135     5       0.3377154644077501         120.0      2.8       
135     6       0.3377154644077501         150.0      4.7

Could anyone help me to solve this?
 
I can able to get the minimum ones, but only the first rows... I need the other rows belonging to the minimum values.

So far I have

Code:
 awk 'min[$1]=="" || min[$1]>$3 {min[$1]=$3; data[$1]=$0}END{for(i in min) print data[i]}' /path/to/infile

The result is

Code:
4       1       0.0518504806582944           0.0      7.3
135     2       0.3377154644077501          30.0      0.9

But this is not correct yet...
 
Try this
Code:
min[$1]=="" || min[$1]>=$3 {
   if (min[$1]==$3) {
     data[$1] = data[$1] "\n" $0
   } else {
     min[$1]=$3
     data[$1]=$0
   }
}
END{for(i in min) print data[i]}

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top