Skip to contents

ac_replace_file() replaces all non-overlapping matches in input files and writes the result to output files.

Usage

ac_replace_file(ac, path, replace_with, output = NULL, stream = FALSE)

Arguments

ac

An <ac_automaton> object created by ac_build().

path

A vector of input file paths to search and replace.

replace_with

A character vector of replacements. If length 1, the same replacement is used for every pattern. Otherwise, it MUST have the same length as ac_patterns(ac), and replacements are matched to patterns by position.

output

A vector of output file paths. It must have the same length as path. If NULL, output paths are created by adding "_replaced" suffix. Existing output files are overwritten.

stream

If FALSE (default), each file is read into memory before replacement. If TRUE, files are searched and replaced as streams. Stream replacement requires an automaton built with match_kind = "standard".

Value

A character vector of output file paths with the same length as path.

Examples

ac <- ac_build(c("fox", "brown", "quick"))
path <- tempfile(fileext = ".txt")
writeLines("The quick brown fox.", path)
ac_replace_file(path = path, ac = ac, replace_with = c("sloth", "grey", "slow"))
#> [1] "/tmp/RtmpuF2ITV/file1d3a3acd5a2c_replaced.txt"