I have a csv file with some lines having more "fields", which are represented between the commas, than others. I need all records to contain the same number of commas. I used the code below to figure out how many commas are in each line, now I need to add however many commas should come at the end. Just not sure how to start.
open (IN, 'C:\Input.csv');
open (OUT,'>C:\Output.csv');
while ($Line4 = <IN>) {
chomp;
$count4 = ($Line4 =~tr/,//);
print OUT "$count4-$Line4";
}
close (IN); close (OUT);
Input(is):
data1,data2,data3,data4
data1,data2,
data1,,data3,data4
data1,data2,data3
Output(needs to be):
data1,data2,data3,data4
data1,data2,,
data1,,data3,data4
data1,data2,data3,
open (IN, 'C:\Input.csv');
open (OUT,'>C:\Output.csv');
while ($Line4 = <IN>) {
chomp;
$count4 = ($Line4 =~tr/,//);
print OUT "$count4-$Line4";
}
close (IN); close (OUT);
Input(is):
data1,data2,data3,data4
data1,data2,
data1,,data3,data4
data1,data2,data3
Output(needs to be):
data1,data2,data3,data4
data1,data2,,
data1,,data3,data4
data1,data2,data3,