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