Need help with a search + replace prog 
Author Message
 Need help with a search + replace prog

I am having a problem with a program I'm trying to write.  I want to make a
search and replace utility for txt files, but I'm not sure how to set up the
program.  Could someone kind of walk me through this?
I'm not quite sure of how to handle the file, would it be easier to use
RANDOM or am I just making more work for myself then I should?

Any help would be greatly appreciated.

P.S. Also if someone could give me the source code of a program like this so
that I could learn, that would be great.

Thanks



Tue, 04 Dec 2001 03:00:00 GMT  
 Need help with a search + replace prog
On Fri, 18 Jun 1999 20:47:35 -0400, "Psyco"

Quote:

>I am having a problem with a program I'm trying to write.  I want to make a
>search and replace utility for txt files, but I'm not sure how to set up the
>program.  Could someone kind of walk me through this?
>I'm not quite sure of how to handle the file, would it be easier to use
>RANDOM or am I just making more work for myself then I should?

>Any help would be greatly appreciated.

>P.S. Also if someone could give me the source code of a program like this so
>that I could learn, that would be great.

>Thanks

Psycho,  (er, Gentle Programmer)

        Please consider the actual code as secondary, a mere detail to be
worked out after the important job of understanding how to solve this
kind of programming problem.

        Always try this for a simple algorithm: Grab a pencil and a piece
of paper and do a bit of it by hand (slowly!!), and carefully note the
steps you took to do it. Now make the dumb computer (which is so good
with numbers!) do that same thing. (or close to it)

        Try putting off the details until the end, but all the critical
points should be in the design of the program early on.

        Now, to specifics: You have lines of text, in a file which you
wish to search within, for replacement.

This is a line of text I want to search for and replace all the "z"'s.

So with my pencil I could just read the line from left to right and
when I found the "z", I could just erase it, and print an "a", and
behold, if that was the whole text file, I'd just have the deed done!

So, in BASIC, we would code this as:

Open "Text_In" for input as #1   ' To Open the text file,

'Note that Random could be used here, but is generally not used with
'text files which lack specific records or fields.

Open "Text_Out" for output as #1 'To write out our new text into.
String_to_replace$ = "z"
String_I_want$ = "a"
changedit% = 0

Do while not eof(1)     'While we have not reached the end of the file
        line input #1, instring$        'Grab a whole line of text and put it
`                                                               in the string "instring$" (catchy
                                                                 name, eh?)    
        'Test the instring$. Does it have the char's you want replaced?
        Do
          if instr(instring$, String$_to_replace$) > 0 then
                'Yep, we got at least one, maybe more.
                'within the line of text, how many char's is it from the left?
                donde_esta% = instr(instring$, String_to_replace$)

                'locate it within the instring$, and replace it.
                mid$(instring$, donde_esta%, len(String_to_replace$) =
                String_I_want$  '(All one line)

                'and I'd want to count how many it found, just to
                'help test it and see if it's accuract. Many times
                'code just needs a wee tweak or two!
                changedit% = changedit% + 1
                locate 10, 15
               print "Found and replaced"; changedit%;"character's "

           else
              changedit% = 0
                ' no more strings to be replaced so:
              break  'get us out of this do loop.
           end if
        Loop                            

'now write out your changed string to your output file
'right about here, before you grab another line of text  to be
'tested for possible replacement of characters.

Loop
' You've reached the end of the text file so
' Close your files
Close #1, #2
print
locate 20, 33
print "End Of Program      "  

It would be nice, Gentle programmer, to add input lines of code, so
the string to search for, and the string you insert, could be given by
the user from the program itself, rather than being pre-set within the
program.

I haven't tried this program, just wrote it off the cuff. If it
bedevils you, just e-mail me direct and I'll assist with any
problemo's.

Dave



Wed, 05 Dec 2001 03:00:00 GMT  
 Need help with a search + replace prog

Quote:

>[snip] I want to make a search and replace utility for txt files,
>but I'm not sure how to set up the program. [snip]

>P.S. Also if someone could give me the source code of a program like
>this so that I could learn, that would be great.

'  **************************************************
'  *                                                *
'  *                  REPTEXT.BAS                   *
'  *                                                *
'  *      Multi-file/multi-text search & replace    *
'  *                                                *
'  *              Judson D. McClendon               *
'  *              Sun Valley Systems                *
'  *              4522 Shadow Ridge Pkwy            *
'  *              Pinson, AL 35126-2192             *
'  *                 205-680-0460                   *
'  *                                                *
'  **************************************************
'  *                                                *
'  *  USAGE: Create two input files: FILES.TXT      *
'  *     and WORDS.TXT.                             *
'  *                                                *
'  *  FILES.TXT is a list of the files to process,  *
'  *     with path if desired. There should be no   *
'  *     spaces or blank lines in this file.        *
'  *                                                *
'  *  WORDS.TXT is a list of the text substitutions *
'  *     to be made, in this form:                  *
'  *                                                *
'  *        "OLDWORD1","NEWWORD1"                   *
'  *        "OLDWORD2","NEWWORD2"                   *
'  *        "OLDWORD3","NEWWORD3"                   *
'  *                                                *
'  **************************************************
DIM NbrWords AS INTEGER
DIM OldWord(1000) AS STRING, NewWord(1000) AS STRING
DIM CmdLine AS STRING, Rec AS STRING, InFile AS STRING, OutFile AS STRING
DIM I AS INTEGER, J AS INTEGER, Ptr AS INTEGER, LenOld AS INTEGER
OPEN "WORDS.TXT" FOR INPUT AS #3
DO WHILE NOT EOF(3)
   NbrWords = NbrWords + 1
   INPUT #3, OldWord(NbrWords), NewWord(NbrWords)
LOOP
CLOSE 3
OPEN "FILES.TXT" FOR INPUT AS #3
DO WHILE NOT EOF(3)
   LINE INPUT #3, InFile
   InFile = LTRIM$(RTRIM$(InFile))
   IF (INSTR(InFile, ".") = 0) THEN
      InFile = InFile + "."
   END IF
   OutFile = LEFT$(InFile, INSTR(InFile, ".") - 1) + ".$$$"
   OPEN InFile FOR INPUT AS #1
   OPEN OutFile FOR OUTPUT AS #2
   DO WHILE NOT EOF(1)
      LINE INPUT #1, Rec
      FOR I = 1 TO NbrWords
         Ptr = INSTR(Rec, OldWord(I))
         IF (Ptr > 0) THEN
            LenOld = LEN(OldWord(I))
            Rec = LEFT$(Rec, Ptr - 1) + NewWord(I) + MID$(Rec, Ptr + LenOld)
         END IF
      NEXT
      PRINT #2, Rec
   LOOP
   CLOSE 1, 2
   KILL InFile
   NAME OutFile AS InFile
LOOP
CLOSE 3
END
--

Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."


Wed, 05 Dec 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. NEED HELP - Search and replace registry script

2. example of our search and replace needs

3. Need VBA to Search for Tabs, Replace with Default Tabs

4. I need a VBScript to search and replace text in a test document

5. Search and replace, how to replace with special characters

6. Help with Search and Replace...

7. search & replace help

8. "HELP!" finding search/replace routine

9. search and replace help...

10. Help needed: Converting PROG to ASCII (HP BASIC 3 or 4)

11. help! need prog's compiled

12. Help, need a qbasic prog compiled!

 

 
Powered by phpBB® Forum Software