Reputation: 321
I would like to count min and max value of my matching line in file:
I have:
xxx 5
xxx 0
xxx 10
yyy 1
yyy 5
yyy 10
And I would like to have on output:
xxx 0 10
yyy 1 10
Minimum and maximum value for xxx
and yyy
matches.
My code is:
awk '{if(min==""){min=max=$2}
if($2>max) {max=$2}
if($2<min) {min=$2}
n[$1]++}
END {for (l in n) {print l "\t" min[l] }}' inputfile
Many thanks for any help.
Upvotes: 0
Views: 374
Reputation: 289575
You need arrays for this:
awk '!($1 in min) {min[$1]=$2; max[$1]=$2}
{max[$1]=(max[$1]<$2?$2:max[$1])
min[$1]=(min[$1]>$2?$2:min[$1])}
END { for (i in max) print i, min[i], max[i]}' file
max[]
contains the maximum value for each value. The same applies to min[]
.
!($1 in min) {min[$1]=$2; max[$1]=$2}
if there is no min
for current field, initialize them.max[$1]=(max[$1]<$2?$2:max[$1])
if the current 2nd field is bigger than the corresponding maximum for the 1st column, replace.min[$1]=(min[$1]>$2?$2:min[$1])
the same with the minimum.END { for (i in max) print i, min[i], max[i]}
loop through the results and print them.$ awk '!($1 in min) {min[$1]=$2; max[$1]=$2} {max[$1]=(max[$1]<$2?$2:max[$1]); min[$1]=(min[$1]>$2?$2:min[$1])} END { for (i in max) print i, min[i], max[i]}' a
yyy 1 10
xxx 0 10
We need to define the min and max when reading the first line!
$ cat a
x -1
x 0
y 1
x -3
$ awk '!($1 in min) {min[$1]=$2; max[$1]=$2} {max[$1]=(max[$1]<$2?$2:max[$1]); min[$1]=(min[$1]>$2?$2:min[$1])} END { for (i in max) print i, min[i], max[i]}' a
x -3 0
y 1 1
Upvotes: 2