Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Create an example file
- fnm <- "foo.txt" # file name
- sink(fnm)
- cat("Hellon## ----worldn")
- sink()
- ## Read the file 'fnm' one line at a time and write it back to 'fnm'
- ## if it does *not* contain the pattern 'pat'
- pat <- "## ----" # pattern
- while(TRUE) {
- rcon <- file(fnm, "r") # read connection
- line <- readLines(rcon, n = 1) # read one line
- close(rcon)
- if(length(line) == 0) { # end of file
- break
- } else {
- if(!grepl(pat, line)) {
- wcon <- file(fnm, "w")
- writeLines(line, con = wcon)
- close(wcon)
- }
- }
- }
- ## Read the file 'fnm' one line at a time and write it back to 'fnm'
- ## if it does *not* contain the pattern 'pat'
- pat <- "## ----" # pattern
- rcon <- file(fnm, "r") # read connection
- wcon <- file(fnm, "w") # write connection
- while(TRUE) {
- line <- readLines(rcon, n = 1) # read one line
- if(length(line) == 0) { # end of file
- break
- } else {
- if(!grepl(pat, line)) {
- writeLines(line, con = wcon)
- }
- }
- }
- close(rcon)
- close(wcon)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement