skan
skan

Reputation: 7760

Modify a file with R at a given position

How can I modify a text file with R at a given position?

It's not a table but any file, for example an xml file.

For example to replace the content of line 122 from columns 7 (to 10) by the content of a variable, that is "3.14".

and update the file.

Imagine that line was

<name=0.32>

now should be

<name=3.14>

Or another option, maybe easier. Look for all aparitions of occurences of "Variable=" and change the next 4 characters.

Upvotes: 1

Views: 49

Answers (1)

Thomas
Thomas

Reputation: 44555

You would have to read in the entire file and then use string manipulation. E.g.,

f <- "file.xml"
x <- readLines(f)
x[122] <- paste0(substring(x[122], 1, 8), "3.14", substring(x[122], 13, nchar(x[122])))
writeLines(x, f)

I assume there is a command-line tool that would be better suited to this.

Upvotes: 1

Related Questions