Input Data into another file

faithless1
faithless1 used Ask the Experts™
on
I have a CSV file (input.csv) containing 50k records with two empty lines seperating every 2K records. I'm looking for a way to insert this data into another CSV file (output) in the 13th column starting on row 5.


Example:

cat input.csv
2000 (1st 2K records)
\n
\n
2000 (2nd 2K records)
\n
\n
2000 (3d 2k records)
etc

The total number of actual records excluding empty lines is 50k. The Data which I need to replace in column 13th in (output.csv) file starts on row 5 and ends on row 50052.

Thank you
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Snippet of the files will be helpful to understand the requirement.

Please provide some rows from the input and some from the output file.

Author

Commented:
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Hi,

I had some issues with firefox and posted duplicate files for output.csv.

I'm interested in inserting data from input.csv to output.csv in column 13 starting on row 5

Thank you in advance
In the output file do you have more columns after column 12? I mean are we looking forward to insert the information between columns?
A basic logic is shown in below assuming that there is no data column 12 onwards and number of records post top 4 rows in output.csv is same.


<?php
$handle = fopen("input.csv", "r");
$handle2 = fopen("output.csv", "r");
//We'll write the output to a third file;
$outhandle = fopen("newoutput.csv", "w");

//Write first 4 lines of output into new output file
$row = 1;
while (($data = fgetcsv($handle2, 1000, ",")) !== FALSE && $row < 5) {
    $csvRow = implode(',', $data);
    fwrite($outhandle, $csvRow);
    $row++;
}

while (($data = fgetcsv($handle2, 1000, ",")) !== FALSE && while (($inputdata = fgetcsv($handle, 1000, ",")) !== FALSE) {
	$outdata = array();
	//push the first 12 records of output.csv into new output
	for ($i=0; $i <=11; $i++) {
		$outdata[$i] = $data[$i];
	}
	//push data from input.csv from 13th column onwards.
	foreach($inputdata as $val) {
		array_push($outdata, $val);
	}

	//Now create the string to be written to output file
	$csvRow = implode(',', $outdata);
	fwrite($outhandle, $csvRow);
}
fclose($outhandle, $handle, $handle2);
?>

Open in new window

Author

Commented:
Hi,

Yes there are records after column 13 and they end at AA so there are 27 records. I will test this out shortly.

Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial