使用R中的日志文件

时间:2017-08-20 22:15:30

标签: r encoding

我有一个.log文件,其数据格式不一致。

数据看起来像这样并存储为" Little-endian UTF-16 Unicode"文字:

2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS
     [XYZ 1000 T1]:1
2017-06-22 01:15:17.945 NOTHING 'D': 989
     [CASE] IN: [ID: 1010]33
     [CASE] IN: [ID: 2010]8
2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS
2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS
2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS

323133.....238813   76378    989899 000000000000

现在,我有几个遵循这种模式的日志文件。我尝试过scan()和read.table(),它们都不会以我期望的格式返回数据。

我期待的数据格式如下:

Date                          String
2017-06-21 00:00:30.483       START THIS THING

但是,我在日志文件中多次使用这些行:

 [CASE] IN: [ID: 1010]33
 [CASE] IN: [ID: 2010]8

而且,

323133.....238813   76378    989899 000000000000

解决此解决方案的最佳方法是什么?谢谢!

1 个答案:

答案 0 :(得分:1)

使用基本R而不进行任何性能优化(例如使用data.table::fread和包lubridate)只是原始草图(忽略时间戳和列名称的时间部分):

log.data <- "2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS
     [XYZ 1000 T1]:1
2017-06-22 01:15:17.945 NOTHING 'D': 989
     [CASE] IN: [ID: 1010]33
     [CASE] IN: [ID: 2010]8
2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS
2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS
2017-06-21 00:00:30.483 START THIS THING
2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS

323133.....238813   76378    989899 000000000000"

log <- read.csv(text = log.data, sep = "\n", header = F)
log$timestamp <- as.Date(log[,1])

这导致:

> log
                                                 V1  timestamp
1    2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS 2017-06-21
2                                   [XYZ 1000 T1]:1       <NA>
3          2017-06-22 01:15:17.945 NOTHING 'D': 989 2017-06-22
4                           [CASE] IN: [ID: 1010]33       <NA>
5                            [CASE] IN: [ID: 2010]8       <NA>
6          2017-06-21 00:00:30.483 START THIS THING 2017-06-21
7    2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS 2017-06-21
8          2017-06-21 00:00:30.483 START THIS THING 2017-06-21
9    2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS 2017-06-21
10         2017-06-21 00:00:30.483 START THIS THING 2017-06-21
11   2017-06-21 00:00:56.400 SOMETHING ELSE HAPPENS 2017-06-21
12 323133.....238813   76378    989899 000000000000       <NA>

更新1:

由于您发现您的日志文件使用UTF-16 little-endian文件编码(在终端中使用Linux / OSX的file命令检查),您必须将文件编码添加到{{1让R在阅读期间正确转换文件内容:

read.csv