Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. ## Create an example file
  2. fnm <- "foo.txt" # file name
  3. sink(fnm)
  4. cat("Hellon## ----worldn")
  5. sink()
  6.  
  7. ## Read the file 'fnm' one line at a time and write it back to 'fnm'
  8. ## if it does *not* contain the pattern 'pat'
  9. pat <- "## ----" # pattern
  10. while(TRUE) {
  11. rcon <- file(fnm, "r") # read connection
  12. line <- readLines(rcon, n = 1) # read one line
  13. close(rcon)
  14. if(length(line) == 0) { # end of file
  15. break
  16. } else {
  17. if(!grepl(pat, line)) {
  18. wcon <- file(fnm, "w")
  19. writeLines(line, con = wcon)
  20. close(wcon)
  21. }
  22. }
  23. }
  24.  
  25. ## Read the file 'fnm' one line at a time and write it back to 'fnm'
  26. ## if it does *not* contain the pattern 'pat'
  27. pat <- "## ----" # pattern
  28. rcon <- file(fnm, "r") # read connection
  29. wcon <- file(fnm, "w") # write connection
  30. while(TRUE) {
  31. line <- readLines(rcon, n = 1) # read one line
  32. if(length(line) == 0) { # end of file
  33. break
  34. } else {
  35. if(!grepl(pat, line)) {
  36. writeLines(line, con = wcon)
  37. }
  38. }
  39. }
  40. close(rcon)
  41. close(wcon)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement