sed_replace: Delete or replace an entire line

Description Usage Arguments Value Author(s) See Also Examples

View source: R/sed_replace.R

Description

Delete or replace an entire line

Usage

1
sed_replace(stream, at, replacement, 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.

at

A vector of integers or a character string that designates where replacement is placed in stream. If at is numeric, it designates the lines (or elements) in stream that will be replaced with replacement. The numeric value(s) of at must be in [1:length(stream)]. If at is a character string, the lines in stream that contain the string at are replaced with replacement.

replacement

A character string of length 1 (vectors not supported), that will be inserted to replace the entire line. Or, if replacement = NULL, the entire line is deleted.

warn

If TRUE, warning messages are produced if replacement fails due to mispecifification of at.

...

Additional named arguments to grep, which are applicable if at is a character string. In other words, grep is used to search for the instances of at.

Value

The new stream with the replacements. If the replacement fails because after is specified incorrectly, stream is returned unchanged.

Author(s)

Landon Sego

See Also

sed_insert, sed_substitute, 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
23
24
25
26
27
28
29
30
################################################################################
# Let's create an example stream we can edit
################################################################################
stream <- c("Here's a line",
           "Here's a line we'll delete",
           "Filler line",
           "A line we'll delete",
           "A line we'll entirely replace",
           "The last line")
as.stream(stream)

# Here's a deletion of lines 1 and 2 using line numbers
stream <- sed_replace(stream, 1:2, NULL)
stream

# Here's a line deletion using a search string
stream <- sed_replace(stream, "A line we'll delete", NULL)
stream

# A line replacement using line numbers
stream <- sed_replace(stream, 2, "A new filler line")
stream

# Here's a line replacement with a search string
stream <- sed_replace(stream, "entirely", "A replacement for the line")
stream

# And we can replace multiple lines too
stream <- sed_replace(stream, "line", "All the same")
stream

rsed documentation built on May 1, 2019, 10:40 p.m.