Hi all R users Ive got a file that contains diffrent settings in the manor of: setting1="value1" setting2="value2" setting3="value3" setting4="value4" . . . What I want to do is open the file and change the value of a specific setting like wanna change setting4="value4" -> setting4="value5" and then save the file again. setting1="value1" setting2="value2" setting3="value3" setting4="value5" . . . -- View this message in context: http://r.789695.n4.nabble.com/Alter-a-line-in-a-file-tp3498187p3498187.html Sent from the R help mailing list archive at Nabble.com.
try this: a <- readLines(textConnection('setting1="value1" setting2="value2" setting3="value3" setting4="value4"')) closeAllConnections() # change values ac <- sub('setting4="value4"', 'setting4="value5"', a) writeLines(ac, con='myFile.txt') On Thu, May 5, 2011 at 8:16 AM, Joel <joda2457 at student.uu.se> wrote:> Hi all R users > > Ive got a file that contains diffrent settings in the manor of: > > setting1="value1" > setting2="value2" > setting3="value3" > setting4="value4" > . > . > . > > What I want to do is open the file and change the value of a specific > setting > like wanna change setting4="value4" -> setting4="value5" and then save the > file again. > > setting1="value1" > setting2="value2" > setting3="value3" > setting4="value5" > . > . > . > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Alter-a-line-in-a-file-tp3498187p3498187.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Hi r-help-bounces at r-project.org napsal dne 05.05.2011 14:16:04:> Joel <joda2457 at student.uu.se> > Odeslal: r-help-bounces at r-project.org > > 05.05.2011 14:16 > > > Hi all R users > > Ive got a file that contains diffrent settings in the manor of:What file, what is its structure, is it some R object or separate file? What did you try and what went wrong? Regards Petr> > setting1="value1" > setting2="value2" > setting3="value3" > setting4="value4" > . > . > . > > What I want to do is open the file and change the value of a specific > setting > like wanna change setting4="value4" -> setting4="value5" and then savethe> file again. > > setting1="value1" > setting2="value2" > setting3="value3" > setting4="value5" > . > . > . > > > > > -- > View this message in context:http://r.789695.n4.nabble.com/Alter-a-line-> in-a-file-tp3498187p3498187.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Well your question is quite general the solution would involve several steps. Probably the easiest solution would be to read the data in as a dataframe (using read.table()) and using the '=' as the separator of the columns. Then change the desired values in the dataframe and save it back as a *.csv file, again using sep='='. Another option would be to read the data as a text string and use regexpressions to replace certain strings. Hope that gets you started Jannis --- Joel <joda2457 at student.uu.se> schrieb am Do, 5.5.2011:> Von: Joel <joda2457 at student.uu.se> > Betreff: [R] Alter a line in a file. > An: r-help at r-project.org > Datum: Donnerstag, 5. Mai, 2011 12:16 Uhr > Hi all R users > > Ive got a file that contains diffrent settings in the manor > of: > > setting1="value1" > setting2="value2" > setting3="value3" > setting4="value4" > . > . > . > > What I want to do is open the file and change the value of > a specific > setting > like wanna change setting4="value4" -> setting4="value5" > and then save the > file again. > > setting1="value1" > setting2="value2" > setting3="value3" > setting4="value5" > . > . > . > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Alter-a-line-in-a-file-tp3498187p3498187.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >