[Experimental]

ATTENTION: Be careful, there is no coming back from the effects of this function.

replace_pattern() search each line of a file and replace a specific pattern when found.

replace_pattern(dir = utils::choose.dir(), pattern, replacement)

Arguments

dir

(optional) a string indicating the directory of the files. This function will look up just for files, directories will not be affected. (default:: utils::choose.dir()).

pattern

A string indicating the pattern to look for. The default interpretation is a regular expression. This parameter will be used on stringr::str_replace_all().

replacement

A string indicating the replacement value. This parameter will be used on stringr::str_replace_all().

Value

An invisible NULL. This function don't aim to return values.

Tip

replace_pattern(
  dir = normalizePath(readClipboard(), "/", mustWork = FALSE),
  pattern = "pattern",
  replacement = "pattern"
)

See also

Other string functions: cutter(), split_by_pattern()

Examples

file_name <- tempfile(tmpdir = tempfile())
dir_name <- dirname(file_name)
dir.create(dir_name)
file.create(file_name)
#> [1] TRUE

con <- file(file_name, "r+")
data <- c("01:00:56", "", "Test", "")
writeLines(data, con = con)
close(con)

replace_pattern(
  dir = dir_name,
  pattern = "([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",
  replacement = ""
  )

con <- file(file_name, "r+")
readLines(con)
#> [1] ""     ""     "Test" ""     ""     ""     "Test" ""    
close(con)