-2

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.


Explanation:

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.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
cgxanth
  • 65
  • 5
  • I can not share the code because there is a company Confidentiality agreement.. The program just reads each line and put values into variables until line ends. i need to cound ',' of each line and add ',,,,,,,' in order to insert the missing variables as null.. – cgxanth May 11 '15 at 12:42
  • http://stackoverflow.com/questions/1603566/count-occurrences-of-a-char-in-plain-text-file This is something similar but i need to count ',' and not replace it.. Just add 49(50 variables so 49 ',') - (count of the current ',' ) in every line – cgxanth May 11 '15 at 12:44

2 Answers2

0

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.

martin
  • 3,149
  • 1
  • 24
  • 35
  • Actually this script is just printing.. I want it to overwrite the data inside the file.. Is it possible without a temp file? – cgxanth May 12 '15 at 08:54
  • @cgxanth Yes, you can write `awk ... file > newfile` and have the content in the newfile (don't overwrite your input file, that doesn't work). Alternatively, gnu awk has some kind of inline editing mode. As a note, I'm unsure if my script skips lines with 50 fields but prints them or if it omits them. You should check that. – martin May 12 '15 at 09:00
0

I've used the same approach, but I noticed that it produces an extra comma at the end, so I tweaked the original to not add a comma at the end:

awk -F, 'NF <= 50 {printf $0; for(i = NF; i < 50; ++i)  printf ","; printf "\n" }'
gurisko
  • 1,172
  • 8
  • 14