fileFindReplace: Find and Replace Text in a File

View source: R/utilsFiles.R

fileFindReplaceR Documentation

Find and Replace Text in a File

Description

This function reads a file and replaces specified patterns with given replacements. The function can work with multiple patterns and replacements, and can also perform case-sensitive or case-insensitive searches.

Usage

fileFindReplace(
  filename,
  pattern,
  replacement,
  word = TRUE,
  ignore.case = FALSE,
  listonly = FALSE
)

Arguments

filename

Character. The name of the file to modify.

pattern

Character vector. A vector of patterns to search for in the file.

replacement

Character vector. A vector of replacements for the patterns found.

word

Logical. If TRUE, only whole words will be replaced. Defaults to TRUE.

ignore.case

Logical. If TRUE, the function performs a case-insensitive search. Defaults to FALSE.

listonly

Logical. If TRUE, instead of modifying the files, a summary of the changes that would be made is displayed.. This is used to check changes before applying them. Defaults to FALSE.

Value

Returns list: name of file assessed = number of changes. The function modifies the file in-place.

See Also

txtFindReplace, xlsxFindReplace

Examples

## Not run: 
fileFindReplace("example.txt", "oldText", "newText")
fileFindReplace("example.txt", c("oldText1", "oldText2"), "newText")

## End(Not run)

Epiconcept-Paris/STRAP-epiuf documentation built on Aug. 5, 2024, 3:41 a.m.