Replace the nth line in a text file

How do I deal with replacing the nth line of a text file in R?

+6
source share
2 answers

To replace the third line of this:

$ cat junk.txt sic transit gloria mundi temeo danoas et dona ferentes 

Do it:

 > latin = readLines("junk.txt",-1) > latin[3]="per ardua ad astra" > writeLines(latin,"junkout.txt") 

and get:

 $ cat junkout.txt sic transit gloria mundi per ardua ad astra et dona ferentes 

You can writeLines(latin,"junk.txt") and overwrite the input file if you want.

+18
source

I do not know if it is possible to change a specific line in a stream file (search in a file), although you have the opportunity to read the file, change the column and write the frame to the file, the read, write functions provide you with what you need.

You can also use read.table() to read a file in a table format, modify a specific line, and then write.table()

you have options like read.csv() and write.csv() and many other options like readLines() .

EDIT

Here is a wiki link for processing files in R

+1
source

Source: https://habr.com/ru/post/921832/


All Articles