0

I have a flatfile (csv delimited) with 60 values that need to be replaced with 60 different new values.

Original_Value1 ---> New_Value1
Original_Value2 ---> New_Value2
Original_Value3 ---> New_Value3
Original_Value4 ---> New_Value4

.... to 60.

The file has over 200k entries with the 60 values that need to be changed. What is the most efficient way of achieveing this?

sr_1436048
  • 143
  • 1
  • 12
  • possible duplicate of [find and replace values in a flat-file using PHP](http://stackoverflow.com/questions/2944080/find-and-replace-values-in-a-flat-file-using-php) – mario Jun 12 '12 at 18:35

4 Answers4

0

Assuming

1) the lengths of the old and new values may differ

2) you dont know which lines contain the values to be changed ahead of time

loop through the file, line by line using fgetcsv() inspect the fields in that line, making replacements as neccesary. write the line back to a different file using fputcsv()

goat
  • 31,486
  • 7
  • 73
  • 96
  • Yes the lengths of the old and new values differ however there is 1 specific column I am looking to change the data. The collumn contains a code that corresponds to a location for a category in a Magento implementation. Current value example is "001094500" should become "\Store\Shoes" Thank you for your responce! – sr_1436048 Jun 12 '12 at 21:07
  • To be honest my technical ability is limited, I am learning. Would appreciate if you could help teh n00b. – sr_1436048 Jun 12 '12 at 22:52
0

Perl. At least that would be my advice, should be able to do it in one line.... something along the lines of

open( IN, " < filename.csv ");
open( OUT, " > output.csv ");

while (<IN>) { # reads in each line of the file
   $line =~ s/Original_Value1/New_Value1/gi;  # g is for global, i is to ignore case
   $line =~ s/Original_Value2/New_Value2/gi;  # g is for global, i is to ignore case
   $line =~ s/Original_Value3/New_Value3/gi;  # g is for global, i is to ignore case
   # ... continue to your values... probably a better way of doing this from a file, but this is quick and dirty, and should work
   print OUT $line; # prints out $line to the output file.
}
close(IN);
close(OUT);

That'd be close anyways, I haven't written perl in quite a while and it could probably be optimized down to a few characters by someone good at PERL golf... =)

Cyric297
  • 69
  • 7
  • Just saw that this might be fore PHP only... if it is, the other answers are going to be more useful... – Cyric297 Jun 12 '12 at 18:39
  • I put PHP, PERL is acceptable. So the script using my example should look like:open( IN, " < filename.csv "); open( OUT, " > output.csv "); while () { $line =~ s/Original_Value1/New_Value1/gi; $line =~ s/Original_Value2/New_Value2/gi; print OUT $line; $line =~ s/Original_Value3/New_Value3/gi;$line =~ s/Original_Value4/New_Value4/gi; } close(IN); close(OUT);Thank you for the reply! – sr_1436048 Jun 12 '12 at 20:58
  • Tried this example and received a 0k output file with only 1 value to change. – sr_1436048 Jun 12 '12 at 21:55
  • well your going to have to output to the file with the print OUT $line each time you want to put something in the file. Also, each iteration through the loop is going to be one line in the file. try doing a print $line in the loop and you'll see the output of what its doing. There is a built in variable called $_ that the line is put into each time. your solution looks like it should work, however put the print OUT $line at the bottom of the loop. – Cyric297 Jun 13 '12 at 18:43
0

Here is the sample code:

$input  = 'input.txt';
$output = 'output.txt';

if ($fpin = fopen($input, 'r')) {
  if ($fpout = fopen($output, 'a')) {
    while ($data = fread($fpin, 1024)) {
      fwrite($fpout, your_replacement_function($data));
    }

    fclose($fpout);
  }

  fclose($fpin);
}
ioseb
  • 16,625
  • 3
  • 33
  • 29
0

Take a look at fgetcsv to read the file line by line and add some code to see if each line contains the string you want to replace.

After reading each line, do what you need to do and write it to a new file. You can use fputcsv to write CSV. Finally delete the old file.

Maybe someone knows of a way to edit the middle of a file? I don't think it's possible.

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • Essentially I am looking to replace data in a specific field based on predetermined values. Thank you for your responce! – sr_1436048 Jun 12 '12 at 21:09