# Find the min and max value of column 3 and print whole row

I have huuuge sets of data consisting of 3 columns. I'd like to be able to find max and min values in the third column (there is mix of zeros and then negative values, so I want to find max negative and min negative value) and then print that particular row. How can I do that?

example input:

``````-6.4 -3.6 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
.
.
.
-2.4 -1.6 -14.50
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
.
.
.
-5.4 -2.6 -4.52
-5.4 -2.6 -4.53
-5.4 -2.6 -4.50
-5.4 -2.6 -0.00
-5.4 -2.6 -0.00
``````

output: min: -2.4 -1.6 -14.50

max: -5.4 -2.6 -4.50

• Give an example of the data and the expected output. – Nasir Riley Jan 5 '19 at 3:55
• I've edited, hope it's clear now! – LonelyStudent Jan 5 '19 at 4:02
• Your expected output doesn't match your data. If the values in column three are all negative then the maximum value should be zero. – Nasir Riley Jan 5 '19 at 4:10
• Unfortunately there are negative zeros, but those data are meaningless. What I wrote is what I need to get. – LonelyStudent Jan 5 '19 at 13:07
• Zero is neither positive nor negative. I understand what you need but it's just stated wrongly. – Nasir Riley Jan 5 '19 at 13:28

## 3 Answers

You've already accepted an answer but here's an easier way:

``````egrep -v "0.00|^\." file.txt | awk 'NR==1 {print "min:",\$0} END{print"max:",\$0}'
``````

Output:

``````min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
``````

-As you don't want the zeroes even though they're actually the largest number, the `egrep -v` command prints all lines except those containing 0.00 and beginning with a "."

-After the above, the lines are already in order from smallest to largest. `awk` then prints the first line with "min:" prepended and the last line with "max:" prepended.

If the values in that column happen to not be in order, you can pipe `sort -k3` (assuming it's the third column you want to sort) after the `egrep` command and then pipe it into the `awk` command after it.

I would suggest doing this in Python. If your data is whitespace-separated, it will be something as simple as:

``````minVal = None
maxVal = None
with open('file') as f:
for line in f:
# default: split on whitespace.
third = int(line.split())
if not minVal:
# initialize the values
minVal = third
maxVal = third
else:
minVal = min(third, minVal)
maxVal = max(third, maxVal)

print("min", minVal)
print("max", maxVal)
``````

EDIT: given that you want the entire row, you should instead store `minRow` and `maxRow`. It the past, when I have had to do something like this, I have used a custom min/max function. Both take a `key` argument to specify a custom function.

You can try with awk :

``````awk -v 'f=1' '
NF==3 {
min = min < \$3 ? min : \$3
if ( min != oldmin ) {
a = \$0
oldmin = min
}
if ( \$3 < 0 ) {
if ( f ) {
max = min
f = 0
}
max = max > \$3 ? max : \$3
}
if ( max != oldmax ) {
oldmax = max
b = \$0
}
}
END {
print "min : " a RS "max : " b
}
' infile
``````