File
1 9
1 10
1 12
1 6
2 4
2 100
2 23
2 101
2 99
3 10
3 998
3 1
3 90
First rows with the same maximum and minimum values are evaluated for output.
Maximum value:
awk ' {if (a[$1]<$2) a[$1]=$2}end{for (i in a) print I,a[i]} ' file
Analytical:
For comparison, the first field of the first row is an array of subscript, if the value is less than the second field, then the value of the second field is assigned to the value of the array, here it is necessary to say that the starting array value is 0, so is less than $ is set, so it is a matter of course to assign the value. To the final output.
Minimum value:
awk '! (A[$1]) {a[$1]=$2} {if (a[$1]>$2) a[$1]=$2} End{for (i in a) print I,a[i]} ' file
Analytical:
The minimum value of the time can not, simply change a symbol, the above less than the number to the greater than the number on the line, because the beginning of the a[$1] value is 0, it is certainly not more than $, this time there is no way to compare, so what I do here is, if the following table of the array is the first occurrence, I assign the second field of this line to the array and then compare it. The final output.