[R] replace a few strings in a text file

baptiste auguie ba208 at exeter.ac.uk
Mon Oct 27 11:24:13 CET 2008


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.


Best regards,

baptiste


(R7.2, MacOS 10.5)

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



More information about the R-help mailing list