txtFindReplace: Find and Replace Text in a Text File

View source: R/utilsFiles.R

txtFindReplaceR Documentation

Find and Replace Text in a Text File

Description

This function reads a text file and replaces specified patterns with given replacements. It allows for multiple patterns and replacements, and can perform case-sensitive or case-insensitive searches. It can also match whole words.

Usage

txtFindReplace(
  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, the function only lists the files that would be modified without actually modifying them. Defaults to FALSE.

Value

Returns list: name of file assessed = number of changes. The function modifies the text file in-place and prints the exact number of changes made.

See Also

fileFindReplace, xlsxFindReplace

Examples

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

## End(Not run)

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