[R] How to create a string containing '\/' to be used with SED?
dwinsemius at comcast.net
Thu Nov 27 15:34:03 CET 2008
What do you mean by "doesn't work" and "the \\" are removed?
> a <- "..\\/path\\/file"
On Nov 26, 2008, at 7:46 PM, ikarus wrote:
> Thanks Sean!!
> I followed you suggestion to use gsub() and it worked perfectly!
> I still can't create a string with inside "\/" (e.g., a <-
> doesn't work, R complains and the \\ are removed), but I don't care,
> gsub() does the same job as sed and without using any system call.
> seanpor wrote:
>> Good morning,
>> You do not need to quote a forward slash / in R, but you do need to
>> a backslash when you're inputting it... so to get a string which
>> contains "blah\/blah"... you need to use "blah\\/blah"
>> Unless this is a very very big file you shouldn't need to go out to
>> as gsub() should work adequately... and probably quicker and
>> cleaner. So
>> something along the lines of.. (UNTESTED!!! since I don't have a
>> reproduceable example)
>> tmp1 <- readLines(configurationFile)
>> tmp1 <- gsub("^instance .*", paste("instance = ", data
>> $instancePath, "/",
>> data$newInstance, sep = ""), tmp1)
>> I'm working on 50mb text files, and doing all sorts of
>> manipulations and I
>> do it all inside R under windows XP... reading a 50mb text file
>> the 100mb network and doing a gsub() on most lines takes an elapsed
>> seconds on this office desktop.
>> ikarus wrote:
>>> Hi guys,
>>> I've been struggling to find a solution to the following issue:
>>> I need to change strings in .ini files that are given in input to a
>>> program whose output is processed by R. The strings to be changed
>>> "instance = /home/TSPFiles/TSPLIB/berlin52.tsp"
>>> I normally use Sed for this kind of things. So, inside R I'd like to
>>> write something like:
>>> command <- paste("sed -i 's/^instance .*/instance = ",
>>> data$newInstance, "/' ", configurationFile, sep
>>> = "")
>>> This will overwrite the line starting with "instance " using
>>> "instance =
>>> In the example I gave, data$instancePath = /home/TSPFiles/TSPLIB/
>>> data$newInstance = berlin52.tsp
>>> The problem is that I need to pass the above path string to sed in
>>> However, I couldn't find a way to "create" such a string in R. I
>>> tried in
>>> several different ways,
>>> but it always complains saying that '\/' is an unrecognized escape!
>>> Any suggestion?
> View this message in context: http://www.nabble.com/How-to-create-a-string-containing-%27%5C-%27-to-be-used-with-SED--tp20694319p20711848.html
> Sent from the R help mailing list archive at Nabble.com.
> R-help at r-project.org mailing list
> 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