6

I have a .xml file that I read with readLines() in R. I would like to know if there is some function that allow me to delete from line 15 to line 18.

I would need of a general command, because I have to repeat the function in loop on the same .xml file, but for the following lines.

CafféSospeso
  • 1,101
  • 3
  • 11
  • 28

1 Answers1

9

readLines creates a character vector where each element is a line from the original file. If you want to remove lines 15 through 18, remove those elements from the vector.

my_new_file = current_file[-(15:18)]

You can then use writeLines to replace the old file with the edited version.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • You can also use the `readr` package with the functions `read_lines` and `write_lines`. – Kerry Jackson Apr 30 '21 at 14:28
  • @KerryJackson yes, probably several other packages too. While `readr` does have some clear feature advantages for delimited files and creating data frames/tibbles, I don't think `read_lines` offers any advantage over `readLines`... – Gregor Thomas Apr 30 '21 at 14:31