We help IT Professionals succeed at work.

how to redo numbers in a log file

Johnny
Johnny asked
on
Medium Priority
426 Views
Last Modified: 2010-04-23
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
Comment
Watch Question

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.
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
sorry took so long.. my computer crashed
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Not a problem. You have a good day.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.