[R] Search&replace string?

Romain b.romain36 at voila.fr
Wed May 28 17:13:58 CEST 2008


Hi there,

I would like to know if it is possible to modify a text file with a R function.
In fact I would like to know if a function "Search & Replace" exists.

My problem is to create config files from a Base file in which I have to modify values of parameters.

My Base File: 

#...
#...

Param1= V1_1

#...

Param2 = V2_1
Param3 = V3_1
#...

What I would like for each created file i: 

#...
#...

Param1= V1_i

#...

Param2 = V2_i
Param3 = V3_i
#...

For the moment my solution is to read each line of the config file, modify if needed the line and recopy it in the new file. But the problem is that my file is about 500 lines and I would like to create a lot of different version of Config_file from my Base file so with this method it takes a long time and i'm sure i can improve it. (For example for 85 differents files I have to wait 10min...)


My dream would be a function like this one :
Function <- function(Base_file, Param_array, Value_array)
{
    file.copy(Base_file, Config_file)
    ...
    for (i in length(Param_array)
    {
         Search&Replace(Config_file, "Param_array[i] = something", "Param_array[i] = Vallue_array[i]")
    }
    ...
}

I hope I have been clear enough. If not, don't hesitate to ask me more details!


Thank you for your help.
Have a good day!

____________________________________________________

Vous aussi bénéficiez d'1 Go de stockage gratuit en ligne avec Voila  
http://macle.voila.fr



More information about the R-help mailing list