replace_in_file()
searches each line of a file and replaces a specific
pattern when found.
Arguments
- file
A
character
vector specifying the path(s) to the file(s) in which to perform the replacement.- pattern
A
character
vector specifying the pattern(s) (regular expressions) to search for. If multiple patterns are provided, each must correspond to a replacement of the same length.- replacement
A
character
vector specifying the replacement value(s) to use. Each element is applied to the corresponding pattern via str_replace_all().
See also
Other file functions:
identify_blank_line_neighbors()
,
normalize_hashtags()
,
normalize_names()
,
peek_csv_file()
,
remove_blank_line_dups()
,
sort_files_by_size()
,
split_file()
,
split_files_by_size()
,
zip_files_by_pattern()
Examples
file <- tempfile()
file.create(file)
#> [1] TRUE
con <- file(file, "r+")
data <- c("01:00:56", "", "Test", "")
writeLines(data, con = con)
close(con)
replace_in_file(
file = file,
pattern = "([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",
replacement = ""
)
con <- file(file, "r+")
readLines(con)
#> [1] "" "" "Test" ""
close(con)