awk and sed script to create one output CSV file

Hi All ,

I would require your help to generate one output file after post processing of one CSV file as stated below

This file is just a small cut from a big file . Big file is having 20000 lines

PATTERN,pat0,pat1,pat2,pat3,pat4,pat5,pat6,pat7,pat8,pat9
U_TOP_LOGIC/ipre_reg_0/Q,0,0,1,1,0,0,1,1,0,0
U_TOP_LOGIC/ipre_reg_6/Q,1,1,0,0,1,1,0,0,1,1
U_TOP_LOGIC/pre_reck_1/Q,1,1,0,1,1,0,0,1,1,0
U_TOP_LOGIC/pre_reg_10/Q,0,1,0,1,1,0,0,1,1,1
U_TOP_LOGIC/pre_reg_11/Q,0,0,1,0,1,0,0,1,0,1

Now , I need to create one output file in which whenever the transition is happening from "0" to "1" or "1" to "0" , the destination pattern should be provided weight 0.25 in an incremental order and the end I need to sum the weight of each pattern

For example , this is the pattern U_TOP_LOGIC/ipre_reg_0/Q,0,0,1,1,0,0,1,1,0,0

pat0 is "0" , pat1 -> "0" , pat2 > "1" , pat3 -> "1" , pat4 -> "0" , pat5 -> "0" , pat6 -> 1 , pat7 -> 1 , pat8 -> 0 , pat9 -> 0

Now when the transition is happening from "0" to "1" , for example it is happening for pat1 to pat2 pattern , pat2 is assigned "0.5" weight , similarly when the transition is happening from "1" to "0" , for example it is happening from pat3 and pat4 then 0.5 is assigned for pat4 ,

This should be the sequence for U_TOP_LOGIC/ipre_reg_0

pat2 -> 0.5 
pat 4 -> 0.5 
pat6 -> 0.5 
pat8 -> 0.5 

Output file is like this for all the patterns

Number                   pat0 pat1 pat2 pat3 pat4 pat5 pat6 pat7 pat8 
+pat9 
U_TOP_LOGIC/ipre_reg_0/Q    0   0   0.5  0    0.5   0   0.5  0    0.5 
+ 0
U_TOP_LOGIC/ipre_reg_6/Q    0   0   0.5  0    0.5   0   0.5  0    0.5 
+ 0 
U_TOP_LOGIC/pre_reck_1/Q    0   0   0.5  0.5  0     0.5 0    0.5  0   
+ 0.5 
U_TOP_LOGIC/pre_reg_10/Q    0   0.5 0.5  0.5  0     0.5  0   0.5  0   
+ 0
U_TOP_LOGIC/pre_reg_11/Q    0   0   0.5 0.5   0.5  0.5   0   0.5  0.5 
+ 0.5 
######################################################################
+########
SUM Of weights              0   0.5 2.5  1.5  1.5  1.5  1   1.5  1.5  
+ 1.5 
######################################################################
+########

Thanks and Regards
Kshitij

Please check your arithmetics for the SUM of pat9.
I can't see where you comply with the specified weight of .25.

Try

awk -F, -vOFS=, '
NR == 1 {
         $1 = sprintf ("%-25s", $1)
         gsub (/,/, "\t")
         print
         next
        }
        {printf "%-25s\t0", $1
         for (i=3; i<=NF; i++)  {WGT = ($i != $(i-1)) * .5
                                 printf "\t%s", WGT
                                 SUM += WGT
                                }
         printf RS
        }
END     {printf "%-25s\t%s", "Sum of weights", 0
         for (i=3; i<=NF; i++) printf "\t%s", SUM
         printf RS
        }
 ' file
PATTERN                         pat0    pat1    pat2    pat3    pat4    pat5    pat6    pat7    pat8    pat9
U_TOP_LOGIC/ipre_reg_0/Q        0       0       0.5     0       0.5     0       0.5     0       0.5     0
U_TOP_LOGIC/ipre_reg_6/Q        0       0       0.5     0       0.5     0       0.5     0       0.5     0
U_TOP_LOGIC/pre_reck_1/Q        0       0       0.5     0.5     0       0.5     0       0.5     0       0.5
U_TOP_LOGIC/pre_reg_10/Q        0       0.5     0.5     0.5     0       0.5     0       0.5     0       0
U_TOP_LOGIC/pre_reg_11/Q        0       0       0.5     0.5     0.5     0.5     0       0.5     0.5     0.5
Sum of weights                  0       0.5     2.5     1.5     1.5     1.5     1       1.5     1.5     1

Correction of eventual formatting deviances are left as an exercise to the gentle reader.

3 Likes

Thanks a lot Rudic

Thanks
Kshitij