awk or sed script to count number of occurrences and creating an average

Hi Friends ,

I am having one problem as stated file .
Having an input CSV file as shown in the code

U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_4_/Q,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_5_/Q,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_3_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0

I need to find the number of times "1" occurs in each line and it should go as the last column in each line
Similarly I need to find the number of times "0" occurs in each line and it should go as the last column in each line
I need to create the average of [ number of times "1" occurs / number of "1" or "0" occurs] in each line
I need to create the average of [ number of times "0" occurs / number of "1" or "0" occurs] in each line

Output file should be something like this

U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,{Number of 1 occurence}, {Number of 0 occurence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_4_/Q,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0,{Number of 1 occurrence}, {Number of 0 occurrence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_5_/Q,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,{Number of 1 occurrence}, {Number of 0 occurrence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_3_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,{Number of 1 occurrence}, {Number of 0 occurrence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}

Could you help me out ?

Thanks
Kshitij :slight_smile:

Are those always ones and zeroes?Always starting from field 2?

Yes Rudic , yes all 1's and 0's starting from field 2

Thanks
Kshitij

Try

awk -F, -vOFS=, '{SUM = 0; C = NF - 1; for (i=2; i<=NF; i++) SUM += $i; print $0, SUM, C - SUM, SUM / C, (C - SUM) / C}' file
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,33,34,0.492537,0.507463
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_4_/Q,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0,33,34,0.492537,0.507463
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_5_/Q,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,32,35,0.477612,0.522388
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_3_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,33,34,0.492537,0.507463

Thanks a lot !