[R] replace a few strings in a text file
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Oct 27 11:47:36 CET 2008
baptiste auguie wrote:
> Dear all,
>
>
> I wrote a wrapper to a FORTRAN program using R. The main program uses
> a text file (~200 lines) as an input describing the simulation to be
> run. I typically generate the file once with the right parameters
> using a combination of file(), paste(), cat(). This is fine, and it
> works well, however I then need to update only a few values in the
> file many times (~200 times, typically). I've used Ruby for this task
> in the past, and I wonder whether there is a simple and efficient way
> to achieve this in R.
>
> Here's a minimal example,
>
>
> myFile <- "test.txt"
> writeHeader <-
> function (out=myFile, N=5, wavelength=0.1)
> {
> output <- file(paste(out), "w")
> headerString <- c("Lorem ipsum dolor sit amet, consectetur adipisicing
> elit,
> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
> nisi ut aliquip ex ea commodo consequat.
> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
> dolore eu fugiat nulla pariatur.
> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
> officia deserunt mollit anim id est laborum.
> Variables", wavelength, N, "
>
> - wavelength
> - ind_refMed
>
> Lorem ipsum dolor sit amet, consectetur adipisicing elit,
> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
> nisi ut aliquip ex ea commodo consequat.
> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
> dolore eu fugiat nulla pariatur.
> Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
> officia deserunt mollit anim id est laborum.
> ")
> cat(paste(headerString), file = output, sep = "\n")
>
> close(output)
> }
>
> writeHeader(out=myFile)
>
> system(paste("cat", myFile))
>
> system.time(sapply(1:200, writeHeader) -> b.quiet)
>
>
> Now for the ruby replacement solution:
>
> #!/usr/bin/ruby -w
> lambda = 0.1
> N = 5
>
> input_file=IO.readlines('test.txt')
> # replace wavelength
> input_file[6]= lambda.to_f
> input_file[7]= N.to_f
> f=File.new("test2.txt","w")
> f.puts input_file
> f.close
>
> I think (unverified) that this approach is more efficient than calling
> the writeHeader() each time. Please do let me know if I'm wrong on
> this. The drawback of using this Ruby script is that I need to know
> the numbers of the lines to be replaced (also, I don't know much in
> Ruby). I'm not sure how I can find this other than manually, as there
> is no regular pattern to look for. Ideally the generating script
> writeHeader() would return these line numbers, I'm not sure how to
> achieve this.
>
> Any comments are welcome.
If there are only 200 lines, why not put them into a character vector,
and edit them there? That's more or less what your Ruby code does. In
R, it would be written
input_file <- readLines("test.txt")
input_file[6] <- sprintf("%f", lambda)
input_file[7] <- sprintf("%f", N)
writeLines("test2.txt", input_file)
but if everything is in R, you don't need the first line.
Duncan Murdoch
More information about the R-help
mailing list