【问题标题】:Parse log file with spaces用空格解析日志文件
【发布时间】:2014-03-02 03:35:43
【问题描述】:

我正在尝试将logfile(另存为*.log 文件的文本文件)解析为Visual Basic 2013 Express'DataGridView。日志文件使用空格作为分隔符,但日志的消息部分中有空格。这是日志文件的布局:

Date Time ID Connection Type_of_message(command/response/status/etc) 消息(可能包含多个空格(不是相同数量的空格) 每条消息))

每个日志条目都是文件中的单独一行。

编辑:这里是an example of the log file

2014-02-03 15:35:29 9900 3 Status: Listing directory [[server folder]]
2014-02-03 15:35:46 9900 3 Status: Invalid character sequence received, disabling UTF-8. Select UTF-8 option in site manager to force UTF-8.
2014-02-03 15:35:46 9900 3 Status: Calculating timezone offset of server...
2014-02-03 15:35:46 9900 3 Command: mtime "[[file name]]"
2014-02-03 15:35:46 9900 3 Response: 1382557913
2014-02-03 15:35:46 9900 3 Status: Timezone offsets: Server: -14400 seconds. Local: -18000 seconds. Difference: -3600 seconds.
2014-02-03 15:36:18 9900 2 Status: Connected to [[server name]]
2014-02-03 15:36:18 9900 1 Status: Starting upload of [[local folder name/file name (includes 3 spaces)]]
2014-02-03 15:36:18 9900 1 Command: cd "[[server folder]]"
2014-02-03 15:36:18 9900 2 Status: Starting upload of [[local folder name/file name (includes 3 spaces)]]
2014-02-03 15:36:18 9900 1 Response: New directory is: "[[server folder]]"
2014-02-03 15:36:18 9900 1 Command: put "[[local folder name/file name (includes 2 spaces)]]"
2014-02-03 15:36:18 9900 2 Command: cd "[[server folder]]"
2014-02-03 15:36:18 9900 1 Status: local: [[local folder name/file name (includes 3 spaces)]]=> remote:[[server folder]]

【问题讨论】:

  • 忽略括号之间的空格
  • 除了空格作为分隔符之外,您的日志文件的格式是什么?举几行为例。
  • @Neolisk 我刚刚编辑了我的问题,并带有指向我的日志文件示例的链接。
  • 始终尝试在问题中包含所有相关信息,不要使用外部链接。请参阅我的编辑。除此之外-检查我的答案-我认为这就是您所需要的。
  • @Neolisk 抱歉,我不认为日志能够像他们为您所做的那样完美地发布,否则我会的。

标签: vb.net parsing datagridview logfile logfile-analysis


【解决方案1】:

它可能不漂亮,但它有效:

Dim line As String = "2014-02-03 15:35:29 9900 3 Status: Listing directory [[server folder]]"
Dim split As String() = line.Split(" "c)
Dim message As String = String.Join(" "c, split.Skip(5).ToArray)

这是假设您总共有 6 列,最后一列是您的消息。

编辑:正如@Andrew Morton 所建议的,拆分可以重写(无 LINQ):

Dim split As String() = line.Split({" "c}, 6, StringSplitOptions.None)

【讨论】:

  • Dim parts = line.Split(" ".ToCharArray(), 5, StringSplitOptions.None) 使用String.Split 重载之一。
  • 没错,我打算只有6列。我会试试你的脚本。
  • @AndrewMorton:我本来是想找这个重载的,但是VS没有给我建议,所以我想我可能记错了什么。感谢您提及 - 包含在编辑中。
  • @AndrewMorton 查看我已经在使用的脚本,我已经在拆分中拥有几乎相同的属性,除了数字(它是 6,而不是 5)For Each col As String In txtlines(rows).Split({delimiter}, 6, StringSplitOptions.None)
  • @CampSoup1988:这就是为什么您应该始终在问题中提供源代码的原因。 5 或 6 取决于您是否要将消息类型保留为消息本身的一部分。
猜你喜欢
  • 1970-01-01
  • 2016-01-09
  • 2021-09-29
  • 2011-04-26
  • 2012-04-03
  • 1970-01-01
  • 2013-01-30
相关资源
最近更新 更多