2

I am trying to parse a logfile (text file saved as a *.log file) into Visual Basic 2013 Express' DataGridView. The log file uses spaces as its delimiter, but the message part of the log has spaces in it. Here is the layout of the log file:

Date Time ID Connection Type_of_message(command/response/status/etc) Message(may include a number of spaces (not the same amount of spaces per message))

Each log entry is a separate line in the file.

EDIT: Here is 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]]
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
CampSoup1988
  • 139
  • 6
  • 21

1 Answers1

2

It may not be pretty, but it works:

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)

This is assuming you have 6 columns in total, and the last one is your message.

EDIT: As suggested by @Andrew Morton, the split can be rewritten (no LINQ):

Dim split As String() = line.Split({" "c}, 6, StringSplitOptions.None)
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • 1
    Or `Dim parts = line.Split(" ".ToCharArray(), 5, StringSplitOptions.None)` using one of the [String.Split](http://msdn.microsoft.com/en-us/library/ms131450%28v=vs.110%29.aspx) overloads. – Andrew Morton Feb 05 '14 at 16:40
  • That is correct, I plan to have only 6 columns. I will give your script a try. – CampSoup1988 Feb 05 '14 at 16:41
  • @AndrewMorton: I was originally looking for this overload, but VS did not suggest it to me, so I thought I may have remembered something wrong. Thanks for mentioning - included in the EDIT. – Victor Zakharov Feb 05 '14 at 16:51
  • @AndrewMorton Looking over the script I was already using, I had nearly the same attributes already in the split, except for the number (which is 6, not 5) `For Each col As String In txtlines(rows).Split({delimiter}, 6, StringSplitOptions.None)` – CampSoup1988 Feb 05 '14 at 17:02
  • @CampSoup1988: This is why you should always provide source code in your question. 5 or 6 depends on whether you want to keep message type as part of the message itself. – Victor Zakharov Feb 05 '14 at 17:20