[R] Search&replace string?

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu May 29 18:00:24 CEST 2008


Perhaps you want to read the files only once, and save those 85 or 
whatever versions you want.
For Search&Replace you might want to take a look at ?gsub and friends.

Uwe Ligges



Romain wrote:
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list