Code:
I have a following inputfile
MT,AP,CDM,TTML,MUM,GS,SUCC,3
MT,AP,CDM,TTSL,AP,GS,FAIL,9
MT,AP,CDM,RCom,MAH,GS,SUCC,3
MT,AP,CDM,RTL,HP,GS,SUCC,1
MT,AP,CDM,Uni,UPE,GS,SUCC,2
MT,AP,CDM,Uni,MUM,GS,SUCC,2
TTSL,AP,GS,MT,MAH,CDM,SUCC,20
TTML,AP,GS,MT,MAH,CDM,FAIL,10
Uni,KOL,GS,MT,DEL,CDM,SUCC,100
Uni,KOL,GS,MT,HAR,CDM,SUCC,200
Need following o/p based on first field & last field. Conditions are as follows
1. if $1 is MT && $4 not between 0 to 9
then sum column $8
2. Also if $1 or $4 is TTSL or TTML then new name of $1 should be TT & if it is Rcom or RTL then new name should be RR & value of $8 to be summed up accordingly
3. $1 not between 0 to 9 & $4 is MT
So o/p should look like
MT,TT,12
MT,RR,4
MT,Uni,4
TT,MT,30
Uni,MT,300
Code:
awk -F, ' {if($1=="MT" && 4 !~ "[0-9]") sum = sum + $8 } END{print $1","sum} ' inputfile