Search code examples
rtextwritetofilereadlines

Write Line to a specific Index in R


How can I write a line to a specific index in a text-file in R?

I use readLines("ini.ini") to read my file (using R. 3.3.1 so this won't work: iniFile)

Then I find my lines that I want to change with "regex" and now want to change it in my file.

Any suggestion on how I can do it? Thanks!


Solution

  • Should be possible by just changing the specific index into your new text and then write it into the same or new file.

    a <- readLines("ini.ini")
    a[my_specific_line_found_by_grep] <- 'new text'
    writeLines(a, 'ini2.ini')