Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Search and replace string across files with R

View gist:1544009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## The following example demonstrates
## how a serach and replace string task
## can be peformed with R across several files
 
## Create two text files with content
filenames <- c( tempfile(), tempfile() )
for( f in filenames ){
cat("We wish you a Merry Christmas!\n\nBest regards\n", file=f)
}
 
## Replace Merry Christmas with Happy New Year
for( f in filenames ){
 
x <- readLines(f)
y <- gsub( "Merry Christmas", "Happy New Year", x )
cat(y, file=f, sep="\n")
 
}
 
## Review output
for( f in filenames ){
cat(readLines(f), sep="\n")
}

Thanks! Very helpful!

Thanks

Fantastic. I used this as a basis to build a helper function that changes file paths from PC environment "\" to Mac environment "/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.