awk outputs the same maximum and minimum values for the first row

Source: Internet
Author: User
Tags first row


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


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


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.