2016-07-26 6 views
0

ログイベントをdata.tableにロードする場合、各ログはtimestampで識別され、いくつかのログには多くの行が含まれます。特定の区切り文字を含むテキストをrにインポート

2016-07-19 00:00:01,421 WARNING Exception happened while transfering for command 
           at java.lang.NumberFormatException 
           at java.lang.Integer.parseInt 
           at java.util.concurrent.Task 

2016-07-19 00:01:01,525 DEBUG Upload all environments 
2016-07-19 00:01:01,720 DEBUG Upload all environments 
2016-07-19 00:02:00,520 WARNING Excpetion happened while transfering for command 
           at java.lang.NumberFormatException 

は私がdata.table次取得したい:

 log 
1 2016-07-19 00:00:01,421 WARNING Exception happened while transfering for command at java.lang.NumberFormatException at java.lang.Integer.parseInt at java.util.concurrent.Task 
2 2016-07-19 00:01:01,525 DEBUG Upload all environments 
3 2016-07-19 00:01:01,720 DEBUG Upload all environments 
4 2016-07-19 00:02:00,520 WARNING Excpetion happened while transfering for command at java.lang.NumberFormatException 

は私が単一行に各ログイベントをアップロードする

は、私は次の.txtファイルを持っています。 data.table内の行を結合し、その後

docs <- read.table("log2.txt",header=FALSE,sep="\n",col.names="log",nrows=1000) 
+0

代わりに 'readLines'を使用してみてください。 – lmo

答えて

0

使用readLinesと::

require(data.table) 

raw = data.table(s = readLines('log.txt')) 
raw = raw[s != ''] 
raw[, s := stringr::str_trim(s)] 
raw[, idx := cumsum(s %like% '^[0-9]{4}')] 
raw[, list(s = paste(s, collapse = ' ')), by = idx] 

編集:私は\n、セパレータを使用しようとした変更年間の正規表現、感謝のコメントを

+0

これは2016年のみ有効です。 ''^[0-9] {4} ''のようなものが良いでしょう。 – Rentrop

+0

ええ、もちろんですが、あなたは考えを得ます:) – sbstn

+0

私はすでに各行を私の 'data.table'の行、' idx'値でグループ行を表示しますか? –

関連する問題