• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

how to redo numbers in a log file

i have a log file

the first field(delimed by a comma) is the record number

in my file i have it auto reset to 1 each night..but on some of my logs its not doing that right so i need to do a matenance type thing and redo the line numbers...

the thing is i have multi line record numbers ie:

1
2
2
3
4
5
5
5
6
6
7
8
9
9
9
 it can be like that...
what im looking for if the second line is something other then a 1 i want it to rebuld the record numbers starting at 1. bare in mind it needs to check if its on the same number and change approprately ie:
23
23
24
25
26
26
26
27
28
28
29
would be
1
1
2
3
4
4
4
5
6
6
7

thanks for any help provided
Johnny
aka Pern
0
Johnny
Asked:
Johnny
  • 2
1 Solution
 
RobertRFreemanCommented:
Just subtract the first number minus 1 (23 -1 = 22) from each number.

23 - (23 - 1) = 1
23 - (23 - 1) = 1
24 - (23 - 1) = 2
etc.
0
 
Fernando SotoCommented:
Hi Johnny;

This should do what you want.

Imports System.IO
Imports System.Text.RegularExpressions


        Dim filename As String = "C:\Temp\LogFile.log"
        Dim nl As String = ControlChars.CrLf
        Dim sr As New StreamReader(filename)
        Dim input() As String = Regex.Split(sr.ReadToEnd(), nl)
        sr.Close()
        Dim lineno As String = input(1).Substring(0, input(1).IndexOf(","c)).Trim()
        Dim temp As String
        Dim curNum As Integer = 1

        If lineno <> "1" Then
            input(1) = curNum.ToString() & input(1).Substring(input(1).IndexOf(","c))
            For i As Integer = 2 To input.Length - 1
                temp = input(i).Substring(0, input(i).IndexOf(","c)).Trim()
                If Not lineno = temp Then
                    lineno = temp
                    curNum += 1
                End If
                input(i) = curNum.ToString() & input(i).Substring(input(i).IndexOf(","c))
            Next
            Dim newLog As String = String.Join(nl, input)
            ' This writes to the same input file. So if you do not want that
            'change the name
            Dim sw As New StreamWriter(filename)
            sw.Write(newLog)
            sw.Close()
        End If

Fernando
0
 
JohnnyAuthor Commented:
sorry took so long.. my computer crashed
0
 
Fernando SotoCommented:
Not a problem. You have a good day.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now