View Full Version : Searching and replacing a string.
How would I search for and replace a string of data in a text file?
Example:
Hello.txt contains multiple strings of "abcdefg".
I want to replace every instance of "abcdefg" with "1234"
Can some explain to me how to do that.
Lorne
02-15-2002, 10:22 AM
What version of Setup Factory are you using? (5 or 6?)
Originally posted by Lorne:
What version of Setup Factory are you using? (5 or 6?)
Version 6.0
csd214
02-16-2002, 03:36 AM
Still waiting for an answer? Try this:
Assign Values
%_OriginalTxt% =
%_ToBeReplaced% = abcdefg
%_Replacement% = 1234
Length of String
Variable = %_LenOrig%, Source = %_OriginalTxt%
Variable = %_LenToBeRepl%, Source = %_ToBeReplaced%
Assign Value
%_LastPossibleStartpos% = %_LenOrig% - %_LenToBeRepl% + 1 (Expression!)
%_CurrPosTxt% = 0
%_NewTxt% = (Empty)
WHILE Condition = %_CurrPosTxt% <= %_LastPossibleStartpos%
Mid String, Variable = %_TxtPortion%, Source = %_OriginalTxt%, Start at = %_CurrPosTxt% - 1, Number of characters = %_LenToBeRepl%
IF %_TxtPortion% = %_ToBeReplaced%
Assign Value %_RightNo% = %_LenOrig% - %_CurrPosTxt% - %_LenToBeRepl% (Expression!)
Right String %_RightPart%, Source = %_OriginalTxt%, Number of characters = %_RightNo%
IF %_NewTxt% = ""
Assign Value%_LeftNo% = %_CurrPosTxt%
Left String %_NewTxt%, Source = %_OriginalTxt%, Number of characters = %_LeftNo%
END IF
Assign Value %_NewTxt% = %_NewTxt% + %_Replacement% (Expression!)
Assign Value %_CurrPosTxt% = %_CurrPosTxt% + %_LenToBeRepl% (Expression!)
GOTO Label AfterMatchJump
END IF
IF !(%_TxtPortion% = %_ToBeReplaced%)
Mid String %_TxtChr%, Source = %_OriginalTxt%, Start at = %_CurrPosTxt% - 1, Number of characters = 1
Assign Value %_NewTxt% = %_NewTxt% + %_TxtChr% (Expression!)
END IF
Assign Value %_CurrPosTxt% = %_CurrPosTxt% + 1 (Expression!)
AfterMatchJump (Label)
END WHILE
Assign Value %_RightNo% = %_LenOrig% - %_CurrPosTxt%
Right String %_RightPart%, Source = %_OriginalTxt%, Number of characters = %_RightNo%
You probably can make the action code more efficient, but I think my suggestion should work (??)
Tip: When testing a source, make use of the Show Dialog to watch the variables (use Yes/No with No = Abort Setup)
Custom Variables: My (bad?) habit is to use underscore as the first character. I think it is preferable when you execute ^B (insert variable).
csd214:
Thanks for the help. But I have to admit I am rather new to this. How do I tell SUF which file to edit and then make the changes and then write the file back to the drive.
As I said in my first post, this is what i have to accomplish;
1) Edit a file by looking for a snip of text and changing it.
2) Then write it back to the drive.
So if someone can tell me with examples how to do this it would be greatly appreceiated. I entered all the data in the previous post by "csd214" but then noticed I had no commands to open and write the files.
Than-You
Lorne
02-21-2002, 09:30 AM
Just use the Read Text File and Write to Text File actions.
Originally posted by Lorne:
Just use the Read Text File and Write to Text File actions.
What if my file is 20k in size. Somewere in this txt file there is a string that is "The cat meowed". I want to locate this text string and change it to "The dog barks loudly".
How would I do that?
What variable would I writing back to the text file and how?
Thank you for all your help.
Lorne
02-21-2002, 09:55 AM
It doesn't matter how big your text file is. http://www.indigorose.com/ubb/images/icons/smile.gif
Use Read Text File to "load" the text file into a variable.
Then use other actions to search the text stored in that variable and replace the string that you want to replace.
Once the string has been replaced, "save" the text file back to disk by using the Write to Text File action to output the contents of the variable.
Note: if you haven't already done so, you might want to read chapters 2, 10, 14 and 15 in the User's Guide. Also be sure to check the command reference for help with the various actions you need to use.
Lorne
02-21-2002, 09:59 AM
This would be a lot easier if SF6 had a "search and replace" text action...it has already been added to the specs for AutoPlay 4, and we'll likely add it to SF6 too in a future release.
Lorne, you are absolutely right. Search and Replace would have been easier.
Use Read Text File to "load" the text file into a variable. [ What variable ]
Then use other actions to search the text stored in that variable and replace the string that you want to replace. [ Can you provide me with details fo how to do this that might be easier to understand than was earlier in the post. ]
Once the string has been replaced, "save" the text file back to disk by using the Write to Text File action to output the contents of the variable. [ Which variable ]
Yes I am sure I am asking alot, but everyones help is greatly appreciated.
Originally posted by Lorne:
This would be a lot easier if SF6 had a "search and replace" text action...it has already been added to the specs for AutoPlay 4, and we'll likely add it to SF6 too in a future release.
Sorry for all the quotes. Was trying to seperate the questions.
Lorne
02-21-2002, 10:54 AM
Don't have time to reply in depth right now, but: you'll use a custom variable, you can call it whatever you like. If you were using csd214's actions, it would be %_OriginalTxt%.
Have you studied the chapters I listed above?
Well I have tried to follow the instructions but it still does not. Either it goes into a continual loop or just plain stops responding. Keep getting resources are low. Tried it on a different pc and the same result.
Could someone paste me a snippet of code that they tried and works. Or just send me a sf6 file that does only that and I will complete it.
Thanks again for everyones help.
------------------
I am still unbable to get the routine to run correctly. I have tried it and either it loops continually or just hangs the system. I appreciate all the help I am receiving. BUt for some unknown reason I am stillnot able to get it to work.
If someone out there has a working routine, I would greatly appreciate it if you could paste it or email a SF6 file that only has that in it.
Thanks again to Lorne and csd214 forall their help so far.
Lorne
02-26-2002, 11:06 AM
Elan, if you post your email address, csd214 has offered to send you a project with his actions example in it.
[This message has been edited by Lorne (edited 02-26-2002).]
Originally posted by Lorne:
Elan, if you post your email address, csd214 has offered to send you a project with his actions example in it.
[This message has been edited by Lorne (edited 02-26-2002).]
Lorne please thank csd214 for his help.
He can email me at ellison@videotron.ca and eellison@stssystems.com
All of your help is greatly appreciated..
Thanks
------------------
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.