How to Perform a Find & Replace via Macro using a source file

How to Perform a Find & Replace via Macro using a source file

24
Basic UserBasic User
24

    Sep 25, 2009#1

    Is there any way either via Macro or via some other feature in UE Studio/UltraEdit to perform a Find & Replace using a custom source list of items where you have 2 columns with the first containing the search text and the second column containing the replace with text?

    I have hundreds of search words to replace with alternate text and trying to create a macro with that many search items listed explicitly results in a very large macro that is difficult to maintain and very prone to duplication and other errors.

    Any comments or suggestions on how to do this would be greatly appreciated.

    6,675585
    Grand MasterGrand Master
    6,675585

      Sep 25, 2009#2

      I have several macros written for such q task. The method is always the same. See for example:

      Replace text in file A with info from file B
      Iterate thru a list of files, each with its own find/replace criteria
      replacing variables with values

      I found this topics by searching with the advanced forum search in the Macro forum only for find replace loop copy.

      If you post how the list file looks, it can write in a few minutes the macro. For example the list file looks like:

      search string 1|replace string 1
      search string 2|replace string 2
      search string 3|replace string 3

      So the list file is a CSV file without specials like double quoted or multi-line fields. The separator character should be a character which surely does never exist in any search and replace string. Possible would be also a tab character, a comma, a semicolon, a paragraph sign or something similar. Also important is to know if only a single replace or a replace all should be executed on the source file and also if the search strings are case sensitive or not.
      Best regards from an UC/UE/UES for Windows user from Austria

      24
      Basic UserBasic User
      24

        Sep 25, 2009#3

        Mofi - Thaks for the links.

        Currently the list of Find & Replace values are in an Excel spreadsheet. I'm open to any methods on doing this with the preference that it be something that add to the list or taking away from the list is as easy as adding and deleteing a line of text and that the items can be kept in a single file. It doesn;t matter if it's a tab delimited file or a CSV file or something else because I can export the list from Excel to a delimited txt file very easily each time I need to provide an update to the list of find & replace words.

        To be honest I was hoping you'd reply with a reference to some feature built into UE/Studio that lets the user reference a source file (some kind of delimited txt based file) that contains a list of words to find and what to replace them with; kind of like adding custom dictionary files.

        I briefly looked at the 3 posts you provided links for. Know now that I'm open to pretty much anything, what method do you suggest to meet the requirements I mention in the first paragraph in this reply?

        Thanks

        6,675585
        Grand MasterGrand Master
        6,675585

          Sep 27, 2009#4

          Okay, the macro below was quickly written and is also very simple. You must have 2 files open when you run this macro, the file to be modified and the CSV file with the search and replace strings. The CSV file with the search and replace strings must have the focus when you execute the macro.

          The search strings must be separated from the replace strings with a comma. It is possible that no replace string exists right the comma. In this case the searched string is just deleted.

          The macro searches case sensitive for the search strings in the list and replaces all occurrences of the searched string by the replace string. Commas in search strings are not allowed and will result in a wrong selection of the search string. Commas in the replace string are no problem.

          The macro as is copies first the entire list of search and replace strings into the other file at top of it. Then it inserts a line with marker character » which just marks where the list ends and where the content of file starts. No search string should start with that character or the macro would stop before the complete list of search/replace strings was processed.

          In a loop with an indefinite number of runs first a check is made if the current line starts with the marker character » which results in exiting the loop. If this is not the case, a regular expression search is used marked from current cursor position (= start of current line) to next comma all characters and copy them to user clipboard 9. This is the search string. Next it sets the cursor right the comma character and selects everything to end of the current line. If nothing is selected, the macro runs the search with an empty replace string to delete all found strings. If a replace string is selected, the macro replaces case sensitive now all occurrences of the search string in clipboard 9 by the currently selected replace string. After every replace all the current line is deleted resulting in setting the cursor to start of the next line where the loop continues.

          After the loop finished the line with the marker character » is deleted, user clipboard 9 is cleared and Windows clipboard is selected again.

          The macro property Continue if search string not found must be checked for this macro.

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          Clipboard 9
          SelectAll
          Copy
          NextWindow
          Top
          Paste
          IfColNumGt 1
          "
          »
          "
          Else

          "
          EndIf
          Top
          Loop
          IfCharIs "»"
          ExitLoop
          EndIf
          Find RegExp "[~,^p]+"
          Copy
          EndSelect
          Key RIGHT ARROW
          StartSelect
          Key END
          EndSelect
          IfSel
          Find MatchCase "^c"
          Replace All "^s"
          Else
          Find MatchCase "^c"
          Replace All ""
          EndIf
          DeleteLine
          EndLoop
          DeleteLine
          ClearClipboard
          Clipboard 0

          Please note: If one of the search strings is contained also in any other search or replace string below in the list, the macro as is would produce a wrong result. For example a list file like this:

          search for,replace with
          second search for word x,second replace with word y


          would result in a wrong result because after processing the first line the second line of the list would have been changed also to

          second replace with word x,second replace with word y

          A modified macro would be required to handle such lists correct. Or you use a script which loads the search and replace strings at once into a string variable and runs the replaces using the strings in the variable. When the list of search/replace strings is long, a script would be definitely the better solution than using a macro. With a script you could also create in the output window a log for this job.
          Best regards from an UC/UE/UES for Windows user from Austria

          24
          Basic UserBasic User
          24

            Sep 28, 2009#5

            Mofi -

            Not that I don't appreciate what you've already taken the time to do but do you have eiether an example or a link to an example of a script like what you refer to in the last few sentences of your prior post? The list of find & replace words is rather long. Basically it consist of every keyword in the Transact-SQL language plus many more proprietary words specific to the product we use.


            BTW - I assume there is no built-in feature in UltraEdit/UEStudio that does this kind of mass find & replace and that you have to use a custom macro or script.

            Thanks Again.

            6,675585
            Grand MasterGrand Master
            6,675585

              Sep 28, 2009#6

              There is no command to run multiple replaces based on a list. I would not have written quickly this macro if such a command exist. I have not seen yet a script for that task. Although writing such a script is not really difficult for a script expert, it needs time, surely more than the few minutes I needed to write this macro, especially when testing and commenting the script. I don't have time in the next days to write this script. Maybe another forum member can help you and developes the script.
              Best regards from an UC/UE/UES for Windows user from Austria

              24
              Basic UserBasic User
              24

                Sep 28, 2009#7

                Mofi - One last question, are there any tutorials on scripting at the level needed to produce something like this?

                The more time I spend on this them more suprised I am that something like this isn't built-in. With each new release it amazes how they can find stuff to add to the product but they do and yet nothing like this exists. I guess its not as widespread a request as I would have thought.

                Thanks

                6,675585
                Grand MasterGrand Master
                6,675585

                  Sep 29, 2009#8

                  YSLGuru wrote:One last question, are there any tutorials on scripting at the level needed to produce something like this?
                  See the sticky (= first) topic in the scripts forum and use the tag list group UE/UES Script Commands in the tag list view in UltraEdit to avoid typical beginners mistakes.
                  Best regards from an UC/UE/UES for Windows user from Austria