Author Topic: Need small piece of code that will search and replace a word in a text file.  (Read 1421 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline IronMan

  • Newbie
  • Posts: 38
I'm needing a small piece of code that will search and replace a word in a text file. Must search the entire file, and replace all entries found. I did have this saved but lost it and can't remember how to do it. Any help appreciated.  Thank you.
« Last Edit: October 14, 2020, 09:22:48 pm by IronMan »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
INSTR will search a string of any length and you can restart where you left off in search.

To replace, checkout MID$ the statement not the function, something like:

p = instr(Filestuff$, searchMe$)
while p
   mid$(Filestuff$, p, len(seachMe$) = replacement$
   p = instr(p+1, Filestuff$, searchMe$)
wend

test a bit.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Just be careful of false positives.

For example, you want to change DOG to CAT in the following:

The dog has been biting the dog’s ass, for eating all the dogfood.



In this case, you only want to change “dog” and not “dog’s” or “dogfood”, so you end up with:

The cat has been biting the dog’s ass, for eating all the dogfood.



The trick here, is to actually change “ dog “ to “ cat “ (see the space before and after?), to rule out any false positives in your string changing. 

Just something to be careful of, and something many worthless profanity filters fail to do...

“ass” is profanity, so it becomes a “***”.

Ergo, we see stuff such as “***istance wanted by a high class ******in for Great ***et Quest”
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Marked as best answer by IronMan on October 15, 2020, 02:25:17 pm

Offline IronMan

  • Newbie
  • Posts: 38
OK guys I got it and wanted to post in the event anyone else ever needs it.

This works perfect!


CALL Replace

PRINT "Complete"

SUB Replace ()
    text$ = ""

    OPEN "FRUIT.txt" FOR INPUT AS #1
    DO WHILE NOT EOF(1)
        LINE INPUT #1, line$
        text$ = text$ + line$ + CHR$(13) + CHR$(10)
    LOOP
    CLOSE #1

    text$ = StrReplace$(text$, "APPLE", "PEAR")
    text$ = LEFT$(text$, LEN(text$) - 1)

    OPEN "FRUIT_2.txt" FOR OUTPUT AS #2
    PRINT #2, text$
    CLOSE #2
END SUB

'$INCLUDE:'source\utilities\strings.bas'

Offline IronMan

  • Newbie
  • Posts: 38
This will search the FRUIT.TXT and replace the word APPLE with PEAR, and re-write to output file.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
This will get your file string faster:

Code: QB64: [Select]
  1.  
  2. 'load a file into a string from GUI Tools 1 TxtBoxes 2019-09-30
  3. FUNCTION fileStr$ (txtFile$)
  4.     IF _FILEEXISTS(txtFile$) THEN
  5.         OPEN txtFile$ FOR BINARY AS #1
  6.         fileStr$ = SPACE$(LOF(1))
  7.         GET #1, , fileStr$
  8.         CLOSE #1
  9.     END IF
  10.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
;-))

Code: QB64: [Select]
  1. what$ = "***istance wanted by a high class ******in for Great ***et"
  2. trade "ass", "***", what$
  3. PRINT what$
  4.  
  5. SUB trade (this$, forThis$, inThis$)
  6.     p = INSTR(inThis$, forThis$)
  7.     WHILE p
  8.         MID$(inThis$, p) = this$
  9.         p = INSTR(p + LEN(this$) - 1, inThis$, forThis$)
  10.     WEND
  11.