In R, I am looking to parse a succession of log data into categorised events.
I have a vector call regex_text, it's a continuous string (line breaks added for clarity here):
21/08/2014 22:58CONTENT_ACCESS.preparing
21/08/2014 23:00EXE_IN.preparing
21/08/2014 23:07CONTENT_ACCESS.preparing
21/08/2014 23:08CONTENT_ACCESS.preparing
21/08/2014 23:12EXE_CO.preparing
21/08/2014 23:28EXE_IN.preparing
21/08/2014 23:29CONTENT_ACCESS.preparing
21/08/2014 23:30CONTENT_ACCESS.preparing
and would like to use regex to process the first and last timestamps of each sequences of 'CONTENT_ACCESS.preparing' and place them into this dataframe:
start_ts stop_ts
1 21/08/2014 22:58 21/08/2014 22:58
start_ts stop_ts
2 21/08/2014 23:07 21/08/2014 23:08
start_ts stop_ts
3 21/08/2014 23:29 21/08/2014 23:30
There could be many repetitions of 'CONTENT_ACCESS.preparing', my example just has two instances with two entries in each.
The code below can be run directly in R and currently outputs a single entry:
start_ts stop_ts
1 21/08/2014 23:07 21/08/2014 23:30
I would like guidance on how to extract two entries, as above
Code:
library(stringr)
options(stringsAsFactors = FALSE)
eventised_session <- data.frame(start_ts=as.character(),
stop_ts=as.character())
regex_text <- "21/08/2014 22:58CONTENT_ACCESS.preparing21/08/2014 23:00EXE_IN.preparing21/08/2014 23:07CONTENT_ACCESS.preparing21/08/2014 23:08CONTENT_ACCESS.preparing21/08/2014 23:12EXE_CO.preparing21/08/2014 23:28EXE_IN.preparing21/08/2014 23:29CONTENT_ACCESS.preparing21/08/2014 23:30CONTENT_ACCESS.preparing"
regex_pattern <- "(\\d{2}\\/\\d{2}\\/\\d{4}\\s\\d{2}\\:\\d{2})(CONTENT_ACCESS\\.preparing)"
if (grepl(regex_pattern, regex_text, perl=TRUE)) {
sm <- str_match_all(regex_text, regex_pattern )
#Get the first and last timestamp in matched sequence
r_start_ts <- sapply(sm, function(x) x[1, 2])
r_stop_ts <- sapply(sm, function(x) x[sapply(sm,nrow), sapply(sm, ncol) - 1])
eventised_session[nrow(eventised_session)+1,] <- c(r_start_ts, r_stop_ts)
print(eventised_session)
}
Are you looking for something like this?
vec = c("21/08/2014 23:07CONTENT_ACCESS.preparing", "21/08/2014 23:08CONTENT_ACCESS.preparing", "21/08/2014 23:12EXE_CO.preparing", "21/08/2014 23:28EXE_IN.preparing", "21/08/2014 23:29CONTENT_ACCESS.preparing", "21/08/2014 23:30CONTENT_ACCESS.preparing")
setNames(data.frame(gsub("[a-z].*","",t(matrix(grep("CONTENT_ACCESS.preparing",vec,value=T),2)),T)),c("Start_ts","Stop_ts"))
Start_ts Stop_ts
1 21/08/2014 23:07 21/08/2014 23:08
2 21/08/2014 23:29 21/08/2014 23:30
If you have the regex_text
as in your code then you could do:
regex_text <- "21/08/2014 23:07CONTENT_ACCESS.preparing21/08/2014 23:08CONTENT_ACCESS.preparing21/08/2014 23:12EXE_CO.preparing21/08/2014 23:28EXE_IN.preparing21/08/2014 23:29CONTENT_ACCESS.preparing21/08/2014 23:30CONTENT_ACCESS.preparing"
a = gsub(".*?(\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2})CONTENT_ACCESS[.]preparing.*?","\\1\n",regex_text)
setNames(data.frame(matrix( unlist(strsplit(a,"\n")),ncol = 2,byrow = T)),c("start_ts","stop_ts"))
start_ts stop_ts
1 21/08/2014 23:07 21/08/2014 23:08
2 21/08/2014 23:29 21/08/2014 23:30
a = gsub("((.*?preparing){2})","\\1\n ",regex_text)
b = read.table(text=gsub("(?<=preparing)(?=\\d+)","|",a,perl=T),sep="|",fill=T,h=F)
d = sub("^(?:(?!CONTENT).)*$|(^.*)CONTENT.*$","\\1",as.matrix(b),perl=T)
subset(data.frame(start_ts = d[,1],stop_ts = ifelse(d[,2]=="",d[,1],d[,2])),start_ts!="")
start_ts stop_ts
1 21/08/2014 22:58 21/08/2014 22:58
2 21/08/2014 23:07 21/08/2014 23:08
4 21/08/2014 23:29 21/08/2014 23:30
you can create a list of matching instances you are looking for and create a loop
for (match in match_list){
#match as in your code
#add into db as in your code
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.