Link to home
Start Free TrialLog in
Avatar of Ekuskowski
Ekuskowski

asked on

need to find and replace numbers within a document and increment by 1

I have a text file which is a deposition transcript ( a simple TXT file ) laid out as follows.

00000
     1 jkfdkjdfjkdfdjfk
     2 sdfkjsdfdjkfdjkfdfkj
     3 dfkldfkldfkldfkldfkldfkl
     4 kldfkdfkdfkdlfkdfldfkl
     5  kfdlkdflkdfklfdklfd
     6 dfjkdfjkfdjdkffjkdfdjk
     7 dfkldfkldfkldfkldfkldfkl
00001
     1 jkfdkjdfjkdfdjfk
     2 sdfkjsdfdjkfdjkfdfkj
     3 dfkldfkldfkldfkldfkldfkl
     4 kldfkdfkdfkdlfkdfldfkl
     5  kfdlkdflkdfklfdklfd
     6 dfjkdfjkfdjdkffjkdfdjk
     7 dfkldfkldfkldfkldfkldfkl
00002
     1 jkfdkjdfjkdfdjfk
     2 sdfkjsdfdjkfdjkfdfkj
     3 dfkldfkldfkldfkldfkldfkl
     4 kldfkdfkdfkdlfkdfldfkl
     5  kfdlkdflkdfklfdklfd
     6 dfjkdfjkfdjdkffjkdfdjk
     7 dfkldfkldfkldfkldfkldfkl

The five digit numbers represent page numbers, the single digits represent line numbers. Each page actually has 25 lines per page and the page numbers actually go up to about 500.

My problem is , whoever created this file started with page 00000 and not page 00001 , so now the program that reads this file is incorrect by one page.

I want to know if anyone has any idea how I could easily fix the page numbering in this txt file.  I was thinking of some kind of advanced find and replace and increment by 1 ?

Any thoughts are appreciated
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Make a copy of the current file for backup

Open a temp file for Output
Open the current file file for Input
Read and ignore the first line
Write 00001 to the temp file using a counter that starts at 00001
Read and write the next 25 lines to the temp file
Read and ignore the next line
Increment the counter by one and write 00002 to the temp file
etcetrta until the current file is at EOF

close both files
Kill the current file
rename the temp file with the current file name

Is this definitely a text file?

Also, must the solution be code, or are you open to a non-code solution?
Avatar of Ekuskowski
Ekuskowski

ASKER

yes it is a text file  
I would prefer a non code solution    I'm not very good with code

U
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You will have to use VBA code unless you want to do multiple Find and Replace operations
Sub MultiReplace()
    Dim i As Integer
    
    For i = 1 To ActiveDocument.Paragraphs.Count Step 8
        ActiveDocument.Paragraphs(i).Range.Text = Format((i / 8) + 1, "00000") & vbCr
    Next i
End Sub

Open in new window


There is some get-you-started advice for  VBA at the end of this article:
https://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/A_7236-Macro-code-to-convert-text-to-fields-in-Microsoft-Word.htm
Code worked perfectly. Thank you
Welcome, glad that helped, thanks for the feedback.

~bp