Searching and replacing a string.

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Elan
    Forum Member
    • Feb 2002
    • 9

    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
    Indigo Rose Staff Member
    • Feb 2001
    • 2729

    #2
    Re: Searching and replacing a string.

    What version of Setup Factory are you using? (5 or 6?)
    --[[ Indigo Rose Software Developer ]]

    Comment

    • Elan
      Forum Member
      • Feb 2002
      • 9

      #3
      Re: Searching and replacing a string.

      Originally posted by Lorne:
      What version of Setup Factory are you using? (5 or 6?)

      Version 6.0

      Comment

      • csd214
        Forum Member
        • Oct 2001
        • 939

        #4
        Re: Searching and replacing a string.

        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).

        Comment

        • Elan
          Forum Member
          • Feb 2002
          • 9

          #5
          Re: Searching and replacing a string.

          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

          Comment

          • Lorne
            Indigo Rose Staff Member
            • Feb 2001
            • 2729

            #6
            Re: Searching and replacing a string.

            Just use the Read Text File and Write to Text File actions.
            --[[ Indigo Rose Software Developer ]]

            Comment

            • Elan
              Forum Member
              • Feb 2002
              • 9

              #7
              Re: Searching and replacing a string.

              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.

              Comment

              • Lorne
                Indigo Rose Staff Member
                • Feb 2001
                • 2729

                #8
                Re: Searching and replacing a string.

                It doesn't matter how big your text file is.

                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.
                --[[ Indigo Rose Software Developer ]]

                Comment

                • Lorne
                  Indigo Rose Staff Member
                  • Feb 2001
                  • 2729

                  #9
                  Re: Searching and replacing a string.

                  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.
                  --[[ Indigo Rose Software Developer ]]

                  Comment

                  • Elan
                    Forum Member
                    • Feb 2002
                    • 9

                    #10
                    Re: Searching and replacing a string.

                    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.

                    Comment

                    • Elan
                      Forum Member
                      • Feb 2002
                      • 9

                      #11
                      Re: Searching and replacing a string.

                      Sorry for all the quotes. Was trying to seperate the questions.

                      Comment

                      • Lorne
                        Indigo Rose Staff Member
                        • Feb 2001
                        • 2729

                        #12
                        Re: Searching and replacing a string.

                        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?
                        --[[ Indigo Rose Software Developer ]]

                        Comment

                        • Elan
                          Forum Member
                          • Feb 2002
                          • 9

                          #13
                          Re: Searching and replacing a string.

                          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.

                          ------------------

                          Comment

                          • Elan
                            Forum Member
                            • Feb 2002
                            • 9

                            #14
                            Re: Searching and replacing a string.

                            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.

                            Comment

                            • Lorne
                              Indigo Rose Staff Member
                              • Feb 2001
                              • 2729

                              #15
                              Re: Searching and replacing a string.

                              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).]
                              --[[ Indigo Rose Software Developer ]]

                              Comment

                              Working...
                              X