I made a program that reads data from a file and inserts them into a table.
Each line has variables and there is a comma between every two variables. Each line is a different row of the table. I want to make a unix script that will open the file with 100 lines and read every line and count ,
that the script will find. After that i want it to edit each line and fill the line with ,
until the count of them is 50.
For example i need each line to have 50 commas (,
). If I read a line and has it has 30 ,
, I want to add additional 20 commas to this line.
(line1)
9,15040113501460,0,b1 0035569144,91 302317960883,0,15040113501460,132,15040614170560,N,0,0,0,0,0,0,0,0,0,0,8,0,0000000000000000,0,0,2,,27,b1 003st69144,1
(line2)
9,15350114601560,0,b1 0033765345,91 304294596921,0,15040113501560,132,15040610170260,N,0,0,0,0,0,0,0,0,0,0,8,0,0000000000000000,0,0,2,,27,b1 0031r69144,1
This is the format of the file. Each line will be like this.
The program expects to read 50 variables. So it expects 49 ,
. When a file is like above and it has less variables i am facing an error. So I need a script in unix to add missing ,
in order to take them as null
.
You could use the following awk code:
awk -F"," 'NF < 50 {printf $0; for(i = NF; i < 50; ++i) printf ","; printf "\n" }' file
produces
9,15040113501460,0,b1 0035569144,91 302317960883,0,15040113501460,132,15040614170560,N,0,0,0,0,0,0,0,0,0,0,8,0,0000000000000000,0,0,2,,27,b1 003st69144,1,,,,,,,,,,,,,,,,,,,,
9,15350114601560,0,b1 0033765345,91 304294596921,0,15040113501560,132,15040610170260,N,0,0,0,0,0,0,0,0,0,0,8,0,0000000000000000,0,0,2,,27,b1 0031r69144,1,,,,,,,,,,,,,,,,,,,,
here, NF
is the number of fields in each line, F
is the field separator. The rule sasys that if you have less then 50 fields (49 commas), we will add those missing to the end.