We help IT Professionals succeed at work.

# how to redo numbers in a log file

on
Medium Priority
426 Views
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

## View Solution Only

Commented:
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 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.

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

Commented:
Not a problem. You have a good day.
##### Thanks for using Experts Exchange.

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