VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
sorting info from .txt file Posted by Fud_____ on 17 Feb 2009 at 8:54 AM
Can anyone tell me how I can sort info from this log file? I am trying to match the player names locted in quotations with thier perspective IP located in paranthesis. I have many files in .log format which I need to go through. I have not been able to do this as a .log file or a .txt file.

Thanks in advance.

The nature of this question is that I want to be able to match the player names to the IP for behavioral purposes. I have enclosed a copy of one of the log files.
Report
Re: sorting info from .txt file Posted by seancampbell on 17 Feb 2009 at 12:10 PM
There was no file included.

I can help you write code to do this, are you familiar with VB.Net coding? If you have already started trying to parse this file, please post up code you are having problems with. If you have not started parsing this file, please copy and past 10-20 lines of data out of it and put it up here.

I can show you how to open the file, parse through the data, explain how it works and how to reuse the data... Parsing standard data is pretty straightforward and tends to be 90% of my work.
Report
Re: sorting info from .txt file Posted by Fud_____ on 17 Feb 2009 at 5:35 PM
2009-02-16 18:09:50 TEAMS JOIN "peirats" player 11 team 1
2009-02-16 18:09:50 JOIN SUCCESS "Rambo" player 12 machine 12 (99.191.138.82:2303) cdkey f52de09dccfe305ba622126f6be72fae
2009-02-16 18:09:50 TEAMS JOIN "Rambo" player 12 team 0
2009-02-16 18:09:51 JOIN SUCCESS "Brian" player 13 machine 13 (189.179.212.165:2303) cdkey 39ad22034962f4f907753ebe1a7e69a6
2009-02-16 18:09:51 TEAMS JOIN "Brian" player 13 team 1
2009-02-16 18:09:51 JOIN SUCCESS "GodsWrath" player 14 machine 14 (216.96.117.89:60048) cdkey 672da0ea432b274056aa9f6d858bf5bc
2009-02-16 18:09:51 TEAMS JOIN "GodsWrath" player 14 team 0
2009-02-16 18:10:12 GRIEF ADDED player 13 "Brian" DEMERITS 1
2009-02-16 18:10:12 KILL "Brian" player 12 KILLED "David[Kets]" player 2
2009-02-16 18:10:13 JOIN SUCCESS "Laura" player 15 machine 15 (190.186.82.54:2303) cdkey 85bfaee1268eb9c5de1938e93fdb5fc5
2009-02-16 18:10:13 TEAMS JOIN "Laura" player 15 team 0
2009-02-16 18:10:15 KILL "ICYHOT" player 5 KILLED "TIRO LOCO" player 4

This is an example from my log file... you can see the two fields in which I need. I just downloaded the VB Express 2008. Programmin is not my thing (yet). I do website and development.

So Ill be diving into this. The first project of mine is going to be something that is functional for me though, LOL. I really need to have this.

Thanks for the guidance.
Report
Re: sorting info from .txt file Posted by seancampbell on 18 Feb 2009 at 8:12 AM
Ok being a nice guy, I will write you some simple code to parse a file and grab out variables. I can help you massage that code to work with your stuff, hopefully along the way you learn from what I have done.

Is this data tab delimited or just space delimited? Do you have access to change the way this log file is pumped out? Might be easier if you separate the data by commas. I am going to make the output file delimited by commas and call it a .csv so you can open it in excel.

I split each line down into pieces by "breaking" it on the space character. The String.Split() command will break a string into an array of string, its a very useful command when working with Log File data.

On the form I put one button and left it with the default name. I copied the data you gave me and pasted it to a text file named Test1.txt and put it in my C root directory. Then I told my subroutine to save it as a file called test2.csv (located in C root as well). You can open in Notepad, or preferably Excel.

If you copy and paste this code into a Form with 1 button (default name) it should work.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        parseFile("c:\test1.txt", "c:\test2.csv")

    End Sub

    Public Sub parseFile(ByVal FileName As String, ByVal NewFileName As String)
        'FileName = the file to be parsed
        'NewFileName is the new file with filtered results
        'We could filter the results and display them in
        'a view with windows controls if you wanted that instead

        Dim FF As Integer = FreeFile() 'This returns an integer value to designate for file open and close
        Dim sLine As String = "" 'This is a temporary var to suck each line in
        Dim newFile As String = "" 'Holds the new file's data

        Try 'Use a try-catch (exception handling) incase any errors occur
            FileOpen(FF, FileName, OpenMode.Input) 'Open FileName for reading

            Do While Not EOF(FF) 'EOF returns a True or False based on if we hit the End of File
                sLine = LineInput(FF) 'This grabs the entire line into a string variable

                Dim LineData() As String = sLine.Split(" ") 'This returns an array of strings tokenized by the space
                'LineData(0) should be the Date
                'LineData(1) should be the Time
                'LineData(2) should say TEAMS, JOIN, GRIEF, or KILL
                'LineData(3) should say JOIN, SUCCESS, ADDED, or a playername if it was KILL
                'LineData(4) should be a playername or the word player
                'You can see that the logic sort of branches here based on LineData(2)
                'So lets dig into this array and get our data out!
                'Firstly, we have to make sure LineData.Length > 1 
                'and that isNothing(LineData) returns False so we don't error

                If Not IsNothing(LineData) AndAlso LineData.Length > 1 Then
                    Select Case Trim(LineData(2))
                        'this is an advanced If statement basically
                        'trim() removes spaces from the ends of string

                        'It seems to me that we are only interested in the case
                        'where LineData(2) = JOIN so that is the only place
                        'I will write code
                        Case "TEAMS"
                            'LineData(2) = TEAMS                        
                        Case "JOIN"
                            'LineData(2) = JOIN
                            'we know LineData(4) will have playername here
                            newFile &= LineData(4).Trim("""") 'This trims a " from the ends of a string.
                            newFile &= "," & LineData(9).Trim("(").Trim(")") 'This trims a ( from both sides, then a ) from both sides
                            newFile &= vbNewLine 'Creates a carrage return
                            'Note that &= is like saying newFile = newFile & ","... etc
                        Case "GRIEF"
                            'LineData(2) = GRIEF
                        Case "KILL"
                            'LineData(2) = KILL
                    End Select
                Else
                    'Blank Line Do Nothing
                End If
            Loop
        Catch ex As Exception
            'Darn something happenned, this will give you some idea:
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error Occurred")
        Finally
            'The loop is done or an error occurred
            'Lets close that file in all cases (Finally gets called after the Try code and Catch code
            FileClose(FF)

        End Try

        FF = FreeFile() 'Reset FF incase the last value is no longer available to us

        Try
            'This is in a seperate try block incase the last one errors
            'we can then atleast get out the data already parsed
            FileOpen(FF, NewFileName, OpenMode.Output) 'Open newFileName for output
            PrintLine(FF, newFile) 'easiest thing
            FileClose(FF)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Error Occurred")
        End Try

        MsgBox("Success!", MsgBoxStyle.OkOnly, "Yea, Praise be to Sean")
    End Sub


Happy Coding!!!
Firesickle.com
Report
Re: sorting info from .txt file Posted by Fud_____ on 21 Feb 2009 at 3:12 PM
This is the best ever, you helped me out immensly. LoL @ you saying "Being the nice guy that I am". Seriously though, It looks like I was on the right track with the way that I was thinking, just didn't quite have the knowledge to complete the task.
I have taken your code and done as you stated, I tried to change it to suit my needs. When I started to debug, I got an error. Further investigation revealed that I had to set my windows preferences to display the windows file extensions (they WERE hidden) as suspected when I changed the file name to match what I needed I changed it to .txt hoping that it would parse the info. When it did not, I opened in excel to find out about the delimited data. It displays as a "square" character. <Ugh> So, I guess I am going to have to get the hex editior and try to find out what "char 20"? makes the square and then try to add that to your coding.
I am learning alot here as you can see. Thanks to you. I will still have some further work to do. Once I can parse the info I am hoping to use PHP to taken the FOO.Txt or (Foo.log - even better) have the PHP call the file and display contents into the browser. This should be a great starting point for my little "DynamicAdminPanel.php"! We'll, See.

Thanks so much. I would have clarified about the parsing earlier, I completely disregarded the fact that what you see from me copy/pasting in the forum is not what you would see from the .log file off the app.

Cant wait to see the latest Post from ya! Thanks Etc !!!!!!
Report
Re: sorting info from .txt file Posted by Fud_____ on 21 Feb 2009 at 10:11 PM
Just to keep you up to date on recent developments... I have currently hexed = Foo.log (the txt file I needed to filter). In doing so the string showed me the "Fields" were defined by a single period character.

Using this discovery, I imported said document into excel by means of Data/txt where I got prompted for delimited data. I selected the "other" option for character data, placed the period character in the box, followed the default prompts through execution, and excel currently is housing my file sorted correctly (for the most part).

Excel got everything right with exception to the IP field, it broke apart the IP over 4 columns with the last column containing the port number of the IP. i can only deduce that this was due to the nature fo having to use the period character on import. It seemingly "remarked" if you will the periods in the IP Field. If you recall from earlier the IP resides in ()'s and the name resides in ""'s.

So, if I can merge the broken up IP into one field and have it display correctly, I will be sitting pretty good. I'll post more as I progress.
Report
Re: sorting info from .txt file Posted by seancampbell on 23 Feb 2009 at 6:04 AM
Awesome! Glad to see you are running with the code. If you need help with anything let me know.
Report
Re: sorting info from .txt file Posted by Fud_____ on 25 Feb 2009 at 10:01 PM
OK, Unforunately Im back to asking or help again. I tried to change your above code to make it work and I just cant seem to get it right. What am I doing wrong? I know the CSV is not a comma it is something like chr(9) or (holding the "alt" key down and hitting number 9. I don'e see in the coding where I would be adding that. I tired a few things but met with array outside of index or some crap like that.

Stuck for now,
Ryan

Ps, Im going to see if I can locate your email address so that I can send you an attachment of the foo.log file, I cant paste or upload anything here as it will only render invisible. Let me know if that is a problem
Report
Re: sorting info from .txt file Posted by seancampbell on 26 Feb 2009 at 6:19 AM
If you click on my name (either from my post, or if u look up my name is listed under moderators) it will take you to my profile. On the right there is an "Email Seancampbell" link, that is a mailto: to my email address.

If each line is not seperated by spaces or commas, you can "split" the line on chr(9) putting chr(9) instead of " " in the split command:

Dim LineData() As String = sLine.Split(chr(9))

Send me the log file, maybe it is more involved then that.
Report
Re: sorting info from .txt file Posted by seancampbell on 27 Feb 2009 at 8:20 AM
Final Solution:

requires:
ListView1 (as ListView)
(Listview has these settings:)
ListView1.View = Details
ListView1.FullRowSelect = True
ListView1.GridLines = True
ListView1.HideSelection = False

Button1 (as button) (.Text = "Open Log File")
Button2 (as button) (.Text = "Save ListView as CSV")
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FO As OpenFileDialog
        FO = New OpenFileDialog
        FO.Filter = "LOG files (*.log)|*.log|Text files (*.txt)|*.txt|All files (*.*)|*.*"


        If FO.ShowDialog() = Windows.Forms.DialogResult.OK Then
            parseFile(FO.FileName)
        End If
    End Sub

    Public Sub parseFile(ByVal fileName As String)

        Dim FF As Integer = FreeFile()
        Dim sLine As String = ""

        'setup the listview for putting items into it.
        ListView1.Items.Clear()
        ListView1.Columns.Clear()
        ListView1.Columns.Add("JOIN Try")
        ListView1.Columns.Add("Player Name")
        ListView1.Columns.Add("Player IP")
        ListView1.Columns.Add("Player Port")
        ListView1.Columns.Add("Player CD Key")

        Try
            FileOpen(FF, fileName, OpenMode.Input)

            Do While Not EOF(FF)
                'I don't know why this line input line is breaking...
                'sLine = LineInput(FF)
                'So I commented it out and used Input(FileNumber, ObjectForDataToGoIn)
                '__________________________
                Input(FF, sLine)
                Dim lineData() As String = sLine.Split(Chr(9))



                If Not IsNothing(lineData) AndAlso lineData.Length > 1 Then

                    Dim c As String = lineData(1) 'This is the command

                    Select Case c.ToUpper
                        Case "OPEN"
                            'This shows the file the log comes from
                            'in lineData(2)
                        Case "KILL"
                        Case "GRIEF"
                        Case "CHAT"
                        Case "TEAMS"
                        Case "QUIT"
                        Case "JOIN"
                            'If the command is JOIN then lets grab info and add
                            'to the ListView

                            Dim xItem As New ListViewItem(lineData(2))
                            xItem.SubItems.Add(lineData(3).Trim(""""))
                            xItem.SubItems.Add(lineData(6).Trim("(").Trim(")").Split(":")(0))
                            xItem.SubItems.Add(lineData(6).Trim("(").Trim(")").Split(":")(1))
                            xItem.SubItems.Add(lineData(7).Substring(5).Trim(" "))
                            ListView1.Items.Add(xItem)

                        Case "BAN"
                        Case "SCORE"
                        Case "PCR_TEAM"
                        Case "PCR_PLAYER"
                        Case "GAMEINFO"
                        Case "RCON"
                        Case "CLOSE"
                            'This shows the file the log continues on
                            'in lineData(2)
                    End Select


                End If
            Loop

        Catch ex As Exception

        Finally
            FileClose(FF)

        End Try


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim SF As SaveFileDialog
        SF = New SaveFileDialog
        SF.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"

        Dim FF As Integer = FreeFile()

        If SF.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                FileOpen(FF, SF.FileName, OpenMode.Output)

                For i As Integer = 0 To ListView1.Items.Count - 1
                    Dim fileLine As String = ListView1.Items(i).SubItems(0).Text
                    fileLine &= "," & ListView1.Items(i).SubItems(1).Text
                    fileLine &= "," & ListView1.Items(i).SubItems(2).Text
                    fileLine &= "," & ListView1.Items(i).SubItems(3).Text
                    fileLine &= "," & ListView1.Items(i).SubItems(4).Text
                    PrintLine(FF, fileLine)
                Next
                MsgBox("Success")
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                FileClose(FF)
            End Try
        End If
    End Sub
End Class
Report
Re: sorting info from .txt file Posted by Fud_____ on 27 Feb 2009 at 1:30 PM
For anyone who doesn't thinks Sean is the Bomb, better
ask somebody!!!

Thanks Sean!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.