sed_substitute: Substitute one string for another

Description Usage Arguments Value Author(s) See Also Examples

View source: R/sed_substitute.R

Description

Substitute one string for another

Usage

1
2
sed_substitute(stream, pattern, replacement, every = TRUE, warn = FALSE,
  ...)

Arguments

stream

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.

pattern

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.

replacement

A character string (vectors not supported), that will be substituted for the pattern. Setting replacement = "" will remove the characters matched to pattern.

every

A logical indicating whether every instance of pattern in each line should be substituted with replacement, in which case gsub is used. If every = FALSE, only the first instance of pattern in each line is substituted, in which case sub is used.

warn

If TRUE, warning messages are produced if substitution fails due to mispecifification of pattern.

...

Additional named arguments to grepl and gsub or sub. grepl is used to verify whether pattern is present in stream. gsub or sub are used to perform the substitution, depending on the value of every.

Value

The new stream with the substitions. If the substitution fails because pattern is not found, stream is returned unchanged.

Author(s)

Landon Sego

See Also

sed_insert, sed_replace, sed_comment, streamEdit

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
################################################################################
# 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)

pnnl/rsed documentation built on May 25, 2019, 10:23 a.m.