awk and sed script to create one output CSV file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk and sed script to create one output CSV file
# 1  
Old 10-03-2019
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

Code:
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

Code:
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
Code:
pat2 -> 0.5 
pat 4 -> 0.5 
pat6 -> 0.5 
pat8 -> 0.5


Output file is like this for all the patterns

Code:
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
# 2  
Old 10-03-2019
Please check your arithmetics for the SUM of pat9.
I can't see where you comply with the specified weight of .25.


Try
Code:
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[i] += WGT
                                }
         printf RS
        }
END     {printf "%-25s\t%s", "Sum of weights", 0
         for (i=3; i<=NF; i++) printf "\t%s", SUM[i]
         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.
These 3 Users Gave Thanks to RudiC For This Post:
# 3  
Old 10-03-2019
Thanks a lot Rudic

Thanks
Kshitij
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse this file using awk and output in CSV format?

My source file looks like this: Cust-Number = "101" Cust-Name="Joe" Cust-Town="London" Cust-hobby="tennis" Cust-purchase="200" Cust-Number = "102" Cust-Name="Mary" Cust-Town="Newyork" Cust-hobby="reading" Cust-purchase="125" Now I want to parse this file (leaving out hobby) and... (10 Replies)
Discussion started by: Balav
10 Replies

2. UNIX for Dummies Questions & Answers

How to Create excel file(.csv) using shell script?

Hi, i have shell script which compiles n number of test cases and execute them one by one. i want to create report in excel through script in which two columns namely "test id" and "release".second column have two subcolumns namely compiles and excutes. so i want first column should display test... (15 Replies)
Discussion started by: RamMore123
15 Replies

3. Shell Programming and Scripting

Script to create a CSV file

I created a script that will go out and so a "/sbin/chkconfig --list | egrep XXX" against a server list that would create an output file like the following example: ---------------------------------------------------------------------------------- SERVER1 RC_Script_1 0:off 1:off 2:off... (4 Replies)
Discussion started by: asnatlas
4 Replies

4. UNIX for Dummies Questions & Answers

Create csv with output filenames and file size

Hello All, Here is seeking a bit of help in trying to solve a problem. I am required to create a csv file as shown below: output.csv -> output_1,output_2,output_3,...,output_<N> filename1:20,filename2:30,filename3:30,...,filename<N>:30 by listing output_1, output_2,... , output<N> as... (3 Replies)
Discussion started by: vkumbhakarna
3 Replies

5. Shell Programming and Scripting

awk/sed/something else for csv file

Hi, I have a filename.csv in which there are 3 colums, ie: Name ; prefixnumber ; number root ; 020 ; 1234567 user1,2,3 ; 070 ; 7654321 What I want is to merge colum 2 and 3 that it becomes 0201234567 or even better +31201234567 so the country number is used and drop the leading 0.... (9 Replies)
Discussion started by: necron
9 Replies

6. Shell Programming and Scripting

Using awk/sed in handling csv file.

Please study the below script and the output Script: echo "Minimum ${host} ${process} response time=${min} ms" >> ${OUTDIR}/${OUTFILE}; echo "Maximum ${host} ${process} response time=${max} ms" >> ${OUTDIR}/${OUTFILE}; echo "Average ${host} ${process} response time=${avg} ms" >>... (0 Replies)
Discussion started by: ajincoep
0 Replies

7. Shell Programming and Scripting

how to create csv file using shell script

I have a file in multiple directory which has some records in the following format File: a/latest.txt , b/latest.txt, c/latest.txt -> Name=Jhon Age=27 Gender=M Street=LA Road Occupation=Service I want to generate a csv file from the above file as follows File: output.csv -> ... (9 Replies)
Discussion started by: rjk2504
9 Replies

8. Shell Programming and Scripting

Need help with a script to process a CSV file using SED and AWK

I get a CSV file every day with 2 columns and multiple rows ex: date1,date2 ( both the fields are varchar fields) This data has to be updated in a table which is being done manually and i want to automate that. 1. I have to select all the data from the prod table( 2 columns { date1,date2}) into... (4 Replies)
Discussion started by: kkb
4 Replies

9. Shell Programming and Scripting

generating a create ddl from a csv file using awk

Hello, I would greatly appreciate some help on the this I have comma delimited file as follows: csv file -------- TEST1,fld1,VARCHAR2,3,,, TEST1,fld2,DATE,,,, TEST1,fld2,VARCHAR2,51,,, TEST1,fld4,VARCHAR2,2,,, TEST1,fld5,NUMBER,4,0,, TEST1,fld6,VARCHAR2,1,,,... (5 Replies)
Discussion started by: jville
5 Replies

10. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies
Login or Register to Ask a Question