How to get the new columns with conditions?

Using the below input file, I extract the minimum and maximum for column 3, using as key column 2, I get also the total times key in column 2 counts. ( column 4 in output file )

Using output file column 4, I want to add 2 columns more ( like output desired )

Column 5 in the fist line starts with 1 and add the value in column 4 (column 6).
for second line, column5 takes the value in column 6(+1), and do the same for line1 (dd the value in column 4 (column 6).

Input file

1222  10000  20002
1222  10000  20003
1222  10000  20004
1222  30000  20010
1222  30000  20011
1222  30000  20012
1222  30000  20013
1222  30000  20014
1222  40000  10000
1222  40000  10001
1222  40000  10002
1222  40000  10003
1222  40000  10004
1222  50000  30000
1222  50000  30001
1222  50000  30002
1222  50000  30003
1222  50000  30004

Code used

awk '{ currKey = $2 }
currKey != prevKey { prt(); min=$3; cnt=0 }
{ prevRec=$0; prevKey=currKey; max=$3; cnt++ }
END { prt() }

function prt(   f) {
    if ( cnt ) {
        split(prevRec,f)
        print f[2], min, max, cnt
    }
}' file1 | sort -k1n
Output from code
10000 20002 20004 3
30000 20010 20014 5
40000 10000 10004 5
50000 30000 30004 5
Output desired
110000 20002 20004 3   1   3
230000 20010 20014 5   4   8
240000 10000 10004 5   9  13
350000 30000 30004 5  14  18

Appreciate your help in advance

$ awk '{ currKey = $2 }
> currKey != prevKey { prt(); min=$3; cnt=0 }
> { prevRec=$0; prevKey=currKey; max=$3; cnt++ }
> END { prt() }
>
> function prt(   f) {
>     if ( cnt ) {
>         split(prevRec,f)
>         print f[2], min, max, cnt
>     }
> }' f | sort -k1n | awk -v var=0 ' { print $0, 1+var, var+$NF; var=var+$NF }  '
10000 20002 20004 3 1 3
30000 20010 20014 5 4 8
40000 10000 10004 5 9 13
50000 30000 30004 5 14 18
1 Like
awk '
{
   if (!col2[$2]++) cols[c++]=$2;
   if (!length(min[$2]) || $3 < min[$2]) min[$2]=$3;
   if (!length(max[$2]) || $3 > max[$2]) max[$2]=$3;
}
END {
   for (i=0; i<c; i++) {
      col6+=col2[cols];
      print cols, min[cols], max[cols], col2[cols], ++col5, col6;
      col5=col6;
   }
}
' file1
2 Likes
awk '
!($2 in t)      {nl = NR; printf max $2 FS $3 FS}
                {t[$2]++; max = $3 FS t[$2] FS nl FS NR RS}
END             {printf max}
' file
1 Like