Script to replace text in a flat file

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?

Answers


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()


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);
}

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.


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... =)


Need Your Help

Confused with pdpotrf arguments

c++ mpi linear-algebra intel-mkl scalapack

I want to do a Cholesky factorization in a distributed environment. For that purpose, I use pdpotrf(). However, I am struggling understanding the parameters needed by the function and they provide ...

Spring 4 response to form submission dosent go thru view resolver. Gives back return string in response

java ajax spring forms spring-4

I inherited a spring 4 web project at work which is configured with Tiles view framework, Spring framework for dispatcher servlet and IOC.