-
Notifications
You must be signed in to change notification settings - Fork 2
/
stream-editor.R
30 lines (29 loc) · 1.16 KB
/
stream-editor.R
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
#' @title stream editor
#' @description R equivalent of GNU-sed
#'
#' @param raw_lines a vector of readLines from the file
#' @param pattern a regex pattern to match
#' @param target target string to be replaced
#' @param replacement replacement string to be substituted
#'
#' @return raw_lines : modified vector of lines
#' @export
#' @examples
#' example_string <- "\\target{} \\not_a_target{}"
#' texor::stream_editor(example_string,"\\s*\\\\target\\{\\}", "\\\\target", "\\\\hit")
stream_editor <- function(raw_lines, pattern, target, replacement) {
break_points <- which(grepl(pattern, raw_lines))
#check for length of break_points to avoid no matches
if (!identical(break_points, integer(0))) {
for (iterator in seq_along(break_points)) {
modification_line <- raw_lines[break_points[iterator]]
modified_line <- gsub(target, replacement, modification_line)
raw_lines[break_points[iterator]] <- modified_line
}
message(paste(
"Found ", length(break_points), "Matches for target :", target))
} else {
message(paste("Found 0 Matches for target : ", target))
}
return(raw_lines)
}