Skip to content

Alter a line in a file.

7 messages · Joel, jim holtman, Jannis +2 more

#
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

r-help-bounces at r-project.org napsal dne 05.05.2011 14:16:04:
What file, what is its structure, is it some R object or separate file? 
What did you try and what went wrong?

Regards
Petr
the
http://r.789695.n4.nabble.com/Alter-a-line-
http://www.R-project.org/posting-guide.html
#
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:
#
jholtman wrote:
Problem is that I dont know the value on all the settings that I wanna
change otherwise that looks like something to continue on.
Petr Pikal wrote:
Just a normal textfile nothing fancy
Ive tried diffrent kind of ways of useing my OS witch is linux by the system
command to do it for me but Im not good enought on regexp to get it to work
properly.

--
View this message in context: http://r.789695.n4.nabble.com/Alter-a-line-in-a-file-tp3498187p3498316.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi

r-help-bounces at r-project.org napsal dne 05.05.2011 15:13:34:
But in that case how would you like to select the setting?
file?
system
work
I read the simple text file by read.table
V1
1 setting1="value1"
2 setting2="value2"
3 setting3="value3"
4 setting4="value4"
[1] 4

Regards
Petr
http://r.789695.n4.nabble.com/Alter-a-line-
http://www.R-project.org/posting-guide.html
3 days later
#
On Thu, 05-May-2011 at 06:13AM -0700, Joel wrote:
|>
|> jholtman wrote:
|> > 
|> > 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')
|> > 
|> 
|> Problem is that I dont know the value on all the settings that I wanna
|> change otherwise that looks like something to continue on.
|> 
|> 
|>
|> Petr Pikal wrote:
|> > 
|> > What file, what is its structure, is it some R object or separate file?
|> > What did you try and what went wrong?
|> > 
|> > Regards
|> > Petr 
|> > 
|> 
|> Just a normal textfile nothing fancy
|> Ive tried diffrent kind of ways of useing my OS witch is linux by
|> the system command to do it for me but Im not good enought on
|> regexp to get it to work properly.

R is great for lots of things.  I even use it for things that someone
more skilled would do using Perl, but in your case, it would be much
easier to use a text editor.  If you can't use Emacs or vi, use nano.
You don't need to know anything about text editors to use the menu
options it has for everything.

HTH