I have a log file that I am trying to parse out and sort. Eventually i will print it out into xml. I have already pulled the file out of the log and read it into memory, i have also parsed the log file into a two dimensional array to be able to sort it. The reason i have to parse and put into a two d array is because of this:
07-01-15 00:00:00 00116: < RCPT TO:<[email protected]>
07-01-15 00:00:00 00120: Connection opened by pool-55-555-55-55. <blah>
07-01-15 00:00:00 00116: > 551 user does not exist
If you see in the file the third set of numbers is out of order.
00116
00120
00116
So what i have done is put those values into one side of the array and put the rest of the file into the other side. I now want to sort on column 0 and then write it back out to the file.
This is what the array would look like:
00116 | 07-01-15 00:00:00 00116: < RCPT TO:<[email protected]>
00120 | 07-01-15 00:00:00 00120: Connection opened by pool-55-555-55-5
00116 | 07-01-15 00:00:00 00116: > 551 user does not exist
The result would look like:
00116 | 07-01-15 00:00:00 00116: < RCPT TO:<[email protected]>
00116 | 07-01-15 00:00:00 00116: > 551 user does not exist
00120 | 07-01-15 00:00:00 00120: Connection opened by pool-55-555-55-5
The Code:
This function Reads the file in and splits it into the two d array:
VB Code:
Private Function OrderFile(ByVal fileLocation As String) Dim fs As System.IO.FileStream Dim sr As StreamReader Try 'We have our variables, lets attempt to open it fs = New System.IO.FileStream(fileLocation, _ FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite) Catch 'Make sure that it exists, and if it doesn't display an error MessageBox.Show("Count not open the file. Make sure '" & fileLocation & _ "' exists in the location shown.") Finally 'It works, but we are going to try one last method If fs.CanRead = True Then 'This is where we would read from the file txtFileDisplay.Text = "File opened!" 'We are done with the file now, so close it fs.Close() End If End Try ''Create the StreamReader and associate the filestream with it Dim myReader As New System.IO.StreamReader(fileLocation) ''Read the entire text, and set it to a string Dim sFileContents As String = myReader.ReadToEnd() ''Print it to the textbox txtFileDisplay.Text = sFileContents ''Close everything when you are finished myReader.Close() ' Split file data into array of lines Dim textReader As New System.IO.StreamReader(fileLocation) Dim WholeLine As String Dim lines(,) As String Dim r As Integer Dim Count = 0 ' WholeLine = textReader.ReadLine() While Not WholeLine.Length <= 0 ReDim Preserve lines(1, Count) lines(0, Count) = WholeLine.Substring(18, 5) r = WholeLine.Length lines(1, Count) = WholeLine.Substring(0, r) Count = Count + 1 WholeLine = textReader.ReadLine() End While Call QuickSort(lines, 0, 0, 0) End Function
This is the Quick Sort to sort the two d array:
VB Code:
Sub QuickSort(ByVal vec, ByVal loBound, ByVal hiBound, ByVal SortField) '==--------------------------------------------------------== '== Sort a 2 dimensional array on SortField == '== == '== This procedure is adapted from the algorithm given in: == '== ~ Data Abstractions & Structures using C++ by ~ == '== ~ Mark Headington and David Riley, pg. 586 ~ == '== Quicksort is the fastest array sorting routine for == '== unordered arrays. Its big O is n log n == '== == '== Parameters: == '== vec - array to be sorted == '== SortField - The field to sort on (2nd dimension value) == '== loBound and hiBound are simply the upper and lower == '== bounds of the array's 1st dimension. It's probably == '== easiest to use the LBound and UBound functions to == '== set these. == '==--------------------------------------------------------== Dim pivot(), loSwap, hiSwap, temp, counter ReDim pivot(UBound(vec, 2)) loSwap = LBound(vec) hiSwap = UBound(vec, 2) '== Two items to sort If hiBound - loBound = 1 Then If vec(loBound, SortField) > vec(hiBound, SortField) Then Call SwapRows(vec, hiBound, loBound) End If '== Three or more items to sort For counter = 0 To UBound(vec, 2) pivot(counter) = vec(Int((loBound + hiBound) / 2), counter) vec(Int((loBound + hiBound) / 2), counter) = vec(loBound, counter) vec(loBound, counter) = pivot(counter) Next loSwap = loBound + 1 hiSwap = hiBound Do '== Find the right loSwap While loSwap < hiSwap And vec(loSwap, Int(SortField)) <= pivot(SortField) loSwap = loSwap + 1 End While '== Find the right hiSwap While vec(hiSwap, SortField) > pivot(Int(SortField)) hiSwap = hiSwap - 1 End While '== Swap values if loSwap is less then hiSwap If loSwap < hiSwap Then Call SwapRows(vec, loSwap, hiSwap) Loop While loSwap < hiSwap For counter = 0 To UBound(vec, 2) vec(loBound, counter) = vec(hiSwap, counter) vec(hiSwap, counter) = pivot(counter) Next '== Recursively call function .. the beauty of Quicksort '== 2 or more items in first section If loBound < (hiSwap - 1) Then Call QuickSort(vec, loBound, hiSwap - 1, Int(SortField)) '== 2 or more items in second section If hiSwap + 1 < hiBound Then Call QuickSort(vec, hiSwap + 1, hiBound, Int(SortField)) Call PrintArray(pivot, 0, 0) End Sub 'QuickSort
Swap Rows Used by QuickSort:
VB Code:
Sub SwapRows(ByVal ary, ByVal row1, ByVal row2) '== This proc swaps two rows of an array Dim x, tempvar For x = 0 To UBound(ary, 2) tempvar = ary(row1, x) ary(row1, x) = ary(row2, x) ary(row2, x) = tempvar Next End Sub 'SwapRows
Print New Array. This is where i am getting an error it looks like the array getting passed to this function isn't created correctly or filling it.
VB Code:
Sub PrintArray(ByVal vec, ByVal lo, ByVal hi) '==-----------------------------------------== '== Print out an array from the lo bound == '== to the hi bound. == '== == '==-----------------------------------------== Dim sw As StreamWriter = File.CreateText("input.txt") Dim i, j lo = LBound(vec) hi = UBound(vec) For i = lo To hi For j = 0 To UBound(vec) sw.Write(vec(i, j)) Next Next sw.Close() End Sub 'PrintArray
