Substitute one string for another
sed_substitute(stream, pattern, replacement, every = TRUE, warn = FALSE,
...)
A character vector, each element typically (but not necessarily) containing the text
from a single line in a file, which can be generated via readLines
.
A character string containing the regular expression that will be used to identify
which the elements (or lines) in stream
that will be substituted using sed_substitute
.
A character string (vectors not supported), that will be substituted
for the pattern
. Setting replacement = ""
will remove the characters matched
to pattern
.
If TRUE
, warning messages are produced if substitution fails due to mispecifification
of pattern
.
The new stream
with the substitions. If the substitution fails because pattern
is not found,
stream
is returned unchanged.
# NOT RUN {
################################################################################
# Let's create an example stream we can edit
################################################################################
stream <- c("Here's a line",
"A line where we'll make a substitution",
"A line where we'll delete 'this'",
"Several nonsense nonsense repeated strings nonsense",
"Another nonsense line with nonsense repeated strings",
"The last line")
as.stream(stream)
# Here's a deletion within the line
stream <- sed_substitute(stream, " 'this'", "")
stream
# Here's a substitution of text
stream <- sed_substitute(stream, "substitution", "correction")
stream
# Show the difference between 'every = TRUE' and 'every = FALSE'
sed_substitute(stream, " nonsense", "", every = TRUE)
sed_substitute(stream, " nonsense", "", every = FALSE)
# }
Run the code above in your browser using DataLab