Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

gsub_file

Search and replace strings in files


Description

These functions provide the "file" version of gsub(), i.e., they perform searching and replacement in files via gsub().

Usage

gsub_file(file, ..., rw_error = TRUE)

gsub_files(files, ...)

gsub_dir(..., dir = ".", recursive = TRUE, ext = NULL, mimetype = ".*")

gsub_ext(ext, ..., dir = ".", recursive = TRUE)

Arguments

file

Path of a single file.

...

For gsub_file(), arguments passed to gsub(). For other functions, arguments passed to gsub_file(). Note that the argument x of gsub() is the content of the file.

rw_error

Whether to signal an error if the file cannot be read or written. If FALSE, the file will be ignored (with a warning).

files

A vector of file paths.

dir

Path to a directory (all files under this directory will be replaced).

recursive

Whether to find files recursively under a directory.

ext

A vector of filename extensions (without the leading periods).

mimetype

A regular expression to filter files based on their MIME types, e.g., '^text/' for plain text files. This requires the mime package.

Note

These functions perform in-place replacement, i.e., the files will be overwritten. Make sure you backup your files in advance, or use version control!

Examples

library(xfun)
f = tempfile()
writeLines(c("hello", "world"), f)
gsub_file(f, "world", "woRld", fixed = TRUE)
readLines(f)

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.