Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 正在将access.log解析为data.frame_Regex_R_Parsing - Fatal编程技术网

Regex 正在将access.log解析为data.frame

Regex 正在将access.log解析为data.frame,regex,r,parsing,Regex,R,Parsing,我想解析R中的access.log。它的形式如下,我想将其放入data.frame中: TIME="2013-07-25T06:28:38+0200" MOBILE_AGENT="0" HTTP_REFERER="-" REQUEST_HOST="www.example.com" APP_ENV="envvar" APP_COUNTRY="US" APP_DEFAULT_LOCATION="New York" REMOTE_ADDR="11.222.33.444" SESSION_ID="rst

我想解析R中的access.log。它的形式如下,我想将其放入data.frame中:

TIME="2013-07-25T06:28:38+0200" MOBILE_AGENT="0" HTTP_REFERER="-" REQUEST_HOST="www.example.com" APP_ENV="envvar" APP_COUNTRY="US" APP_DEFAULT_LOCATION="New York" REMOTE_ADDR="11.222.33.444" SESSION_ID="rstg35tsdf56tdg3" REQUEST_URI="/get/me/something" HTTP_USER_AGENT="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" REQUEST_METHOD="GET" REWRITTEN_REQUEST_URI="/index.php?url=/get/me/something" STATUS="200" RESPONSE_TIME="155,860ms" PEAK_MEMORY="18965" CPU="99,99"
日志是每个文件400MB,目前我有大约4GB的日志,所以大小很重要

还有一件事。。有两种不同的日志结构(包括不同的列),因此您不能假定总是有相同的列,但可以假定一次只解析一种结构

到目前为止,我拥有的是此结构的正则表达式:

(\\w+)[=][\"](.*?)[\"][ ]{0,1}
我可以使用
readlines
gsub
read.table
读取数据并以某种方式将其放入数据框中,但速度慢且凌乱


有什么想法吗?Tnx

您可以这样做,例如:

text <- readLines(textConnection(text))
## since we can't use = as splitter (used in url) I create a new splitter
dd   <- read.table(text=gsub('="','|"',text),sep=' ')
## use data.table since it is faster to apply operation by columns and bind them again
library(data.table)
DT <- as.data.table(dd)
DT.split <- DT[,lapply(.SD,function(x) 
             unlist(strsplit(as.character(x) ,"|",fixed=TRUE)))]
DT.split[c(F,T)]

text您可以这样做,例如:

text <- readLines(textConnection(text))
## since we can't use = as splitter (used in url) I create a new splitter
dd   <- read.table(text=gsub('="','|"',text),sep=' ')
## use data.table since it is faster to apply operation by columns and bind them again
library(data.table)
DT <- as.data.table(dd)
DT.split <- DT[,lapply(.SD,function(x) 
             unlist(strsplit(as.character(x) ,"|",fixed=TRUE)))]
DT.split[c(F,T)]

HTTP\u USER\u代理中的
文本您有空格字符,因此不会出现这种情况work@frinx不,对我来说很好。空格位于引号之间,因此它不会被
read.table
检测到。。tnx,让我来try@frinx酷。我很想知道这对您的真实数据是否有效。您提供的代码与我的代码非常相似,但您给了我一个使用read.table的好提示。我只能通过在HTTP\u USER\u代理中使用
x来读取所有内容,因为您有空格字符,所以不会出现这种情况work@frinx不,对我来说很好。空格位于引号之间,因此它不会被
read.table
检测到。。tnx,让我来try@frinx酷。我很想知道这对您的真实数据是否有效。您提供的代码与我的代码非常相似,但您给了我一个使用read.table的好提示。我只能用
x阅读所有内容