Here's an approach using R, where you create a function that finds-and-replaces text in a file and you apply this function to all of the R scripts in your directory.
In the example below, this changes the code color = 'green'
in any R script to color = 'blue'
.
# Define function to find-and-replace text in a single file
file_find_replace <- function(filepath, pattern, replacement) {
file_contents <- readLines(filepath)
updated_contents <- gsub(x = file_contents, pattern = pattern, replacement = replacement)
cat(updated_contents, file = filepath, sep = "\n")
}
# Apply the function to each of the R scripts in the directory
my_r_scripts <- list.files(path = my_dir, pattern = "(r|R)$")
for (r_script in my_r_scripts ) {
file_find_replace(r_script,
"color = 'green'",
"color = 'blue'")
}