Multiple find and replace from a data file

Multiple find and replace from a data file

12
Basic UserBasic User
12

    Mar 27, 2012#1

    I have an SQL file which has data like this:

    Code: Select all

    (1, orange),
    (2, blue),
    (3, green),
    (4, yellow),
    And there is a second file which has data like this:

    Code: Select all

    Green
    Blue
    Yellow
    Yellow
    green
    Blue
    Blue
    Orange
    Green
    I am looking for a script which could replace the data in my second file to the numbers in the SQL file like this:

    Code: Select all

    3
    2
    4
    4
    3
    2
    2
    1
    3
    Is there any easy way to do this?

    6,603548
    Grand MasterGrand Master
    6,603548

      Mar 27, 2012#2

      Here is a script tested on your sample lines. Please read the comments of the script before you execute the script the first time.

      Code: Select all

      if (UltraEdit.document.length > 1) // At least 2 files must be opened.
      {
         // Define the environment for the script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
      
         // The active file is the file to modify with the numbers
         // stored for every color line by line in the other opened file.
         var nListFile = (UltraEdit.activeDocument.path == UltraEdit.document[0].path) ? 1 : 0;
         // Select everything in list file with DOS terminated lines.
         UltraEdit.document[nListFile].selectAll();
         if (UltraEdit.document[nListFile].isSel())
         {
            // Get all lines in the file into an array of strings with the line termination.
            var asStrings = UltraEdit.document[nListFile].selection.split("\r\n");
            // Discard the selection in the list file by moving caret to top of file.
            UltraEdit.document[nListFile].top();
      
            // Now the color names can be replaced by the numbers in active document.
            UltraEdit.ueReOn();
            UltraEdit.activeDocument.findReplace.mode=0;
            UltraEdit.activeDocument.findReplace.matchCase=false;
            UltraEdit.activeDocument.findReplace.matchWord=true;
            UltraEdit.activeDocument.findReplace.regExp=false;
            UltraEdit.activeDocument.findReplace.searchDown=true;
            UltraEdit.activeDocument.findReplace.preserveCase=false;
            UltraEdit.activeDocument.findReplace.replaceAll=true;
            UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
            if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
            {
               UltraEdit.activeDocument.findReplace.searchInColumn=false;
            }
            // Move caret in active file to top of the file.
            UltraEdit.activeDocument.top();
      
            // The following loop is run for every string in the array.
            for (var nStringIndex = 0; nStringIndex < asStrings.length; nStringIndex++ )
            {
               // Ignore empty strings caused by empty lines in the list file.
               if (asStrings[nStringIndex].length == 0) continue;
               // Get number for color.
               var sNumber = asStrings[nStringIndex].replace(/^\([ \t]*(\d+).+$/,"$1");
               // Get color name.
               var sColor = asStrings[nStringIndex].replace(/^.+,[ \t]*(\w+).+$/,"$1");
               // Replace all occurrences of current color by the number.
               UltraEdit.activeDocument.findReplace.replace(sColor, sNumber);
            }
         }
      }

      12
      Basic UserBasic User
      12

        Mar 28, 2012#3

        Wow, it worked like a charm. Thanks.

        2
        NewbieNewbie
        2

          Another variation of find and replace from a data file

          Mar 27, 2019#4

          I found this subject which was close to what I need. I use 64 bit UE 24.10.0.32 at the moment. The high level summary description is as follows:

          I have a text file containing a list of ID strings. Let's call it the ID file.
          Go line by line through the ID file. Each line contains an ID string.
          1. Start at line 1 in the ID file and search an entire directory of files for the ID string at line 1.
          2. For each match found in any file, append the suffix ' _ID'  to the matching string in the file.
          3. Also on the same line in the file where we appended the _ID suffix, replace every occurrence of SP_Dev with ER_Dev.
          4. Repeat steps 2 and 3 for every line in the ID file until the end of the ID file is reached.
          The following is an actual "snippet" from an ID file for an example.
          The IDs are bracketed by the '{' and the first ','  delimiters. Ignore the rest of the junk in the line

          Code: Select all

           { "ER_FW_VERSION_NUMBER",              0x15062917, BADDS_MASK, 0,       0 }, \
           /* memory map */ \
           { "ER_FW_VERSION",                     0xA0000000, BADDS_RO,   4,       0x15062917 }, \
           { "ER_FW_VERSION_YEAR",                0xFF000000, BADDS_MASK, 31,      24 }, \
           { "ER_FW_VERSION_MONTH",               0x00FF0000, BADDS_MASK, 23,      16 }, \
           { "ER_FW_VERSION_DAY",                 0x0000FF00, BADDS_MASK, 15,      8 }, \
           { "ER_FW_VERSION_VERSION",             0x000000F0, BADDS_MASK, 7,       4 }, \
           { "ER_FW_VERSION_REVISION",            0x0000000F, BADDS_MASK, 3,       0 }, \
           { "ER_COMMON_SECTION_ADDR",            0xA0000000, BADDS_RW,   500,     0x00000000 }, \
          
          So the first ID would be ER_FW_VERSION_NUMBER, the second ID would be ER_FW_VERSION and so forth.

          The typical lines in files with matched IDs will be in one of the following forms:

          Code: Select all

          foo = SP_DevPort::read32(SP_DevPort::ER_FW_VERSION_NUMBER_);
          SP_DevPort::write32(SP_DevPort::ER_FW_VERSION_NUMBER_,MmwBlah::11);
          
          There will always be two SP_Dev strings in lines with matched IDs preceding the matched ID. The matched IDs in the file will have an underscore suffix. The resulting ID name ER_FW_VERSION_NUMBER__ID (two underscores) or ER_FW_VERSION_NUMBER_ID (one underscore) is fine.

          And there is one more task requirement. Using the same ID file, do the following:
          1. In the ID file, find first ID which is bracketed with " ",
          2. In the current line, search for: 0x
          3. Grab all 10 characters beginning with 0x and use as the search string.
          4. In all files in the directory, replace the 10 characters (beginning with 0x) with the ID name plus _ID suffix
          5. Repeat until end of ID file is reached.
          So for example in the first line with an ID which is:

          Code: Select all

          { "ER_FW_VERSION_NUMBER",              0x15062917, BADDS_MASK, 0,       0 }, \
          We search all the files in the directory for 0x15062917 and replace all occurrences with ER_FW_VERSION_NUMBER_ID.

          This would be a huge timer saver for us. Thanks in advance.

          6,603548
          Grand MasterGrand Master
          6,603548

            Re: Another variation of find and replace from a data file

            Mar 28, 2019#5

            I suggest the following script for this task on which at least the line with C:\\Temp\\Test\\ must be adapted to real directory path. Each backslash in real directory path must be escaped with one more backslash in JavaScript string with directory path. And the directory path must end with a backslash. The file type specification in next line can be also adapted to reduce the number of files on which the replace in files is performed by the script.

            Code: Select all

            function LengthSort (sID1, sID2)
            {
               return sID2.length - sID1.length;
            }
            
            if (UltraEdit.document.length > 0)  // Is any file opened?
            {
               // Define environment for this script.
               UltraEdit.insertMode();
               if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
               else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            
               // Get all IDs from file into an array of strings.
               UltraEdit.activeDocument.selectAll();
               // Is the active file not an empty file?
               if (UltraEdit.activeDocument.isSel())
               {
                  var asIDs = UltraEdit.activeDocument.selection.match(/{[\t ]+".+?"/g);
                  var asAddr = UltraEdit.activeDocument.selection.match(/{[\t ]+".+?"[\t ,]+0x[0-9A-Fa-f]+/g);
                  UltraEdit.activeDocument.top();  // Just to cancel the selection.
            
                  if(asIDs)   // Was at least one ID found?
                  {
                     // Remove the characters around each ID.
                     for (var nID = 0; nID < asIDs.length; nID++)
                     {
                        asIDs[nID] = asIDs[nID].replace(/^{[\t ]+"(.+?)"/,"$1");
                     }
            
                     // Sort the identifies according to their string lengths because
                     // of some identifiers are substrings of longer identifiers. That
                     // would not be really needed, but it is in general safer.
                     asIDs.sort(LengthSort);
            
                     // Join the IDs to one string with OR operator | between.
                     var sAllIDs = asIDs.join("|");
                     UltraEdit.outputWindow.write("The IDs are: "+sAllIDs);
            
                     UltraEdit.perlReOn();
                     UltraEdit.frInFiles.directoryStart="C:\\Temp\\Test\\";
                     UltraEdit.frInFiles.searchInFilesTypes="*";
                     UltraEdit.frInFiles.searchSubs=false;
                     UltraEdit.frInFiles.filesToSearch=0;
                     UltraEdit.frInFiles.ignoreHiddenSubs=true;
                     UltraEdit.frInFiles.logChanges=true;
                     UltraEdit.frInFiles.matchCase=true;
                     UltraEdit.frInFiles.matchWord=false;
                     UltraEdit.frInFiles.openMatchingFiles=false;
                     UltraEdit.frInFiles.preserveCase=false;
                     UltraEdit.frInFiles.preserveCase=false;
                     UltraEdit.frInFiles.unicodeSearch=false;
                     UltraEdit.frInFiles.useEncoding=false;
                     UltraEdit.frInFiles.regExp=true;
            
                     // Run a case-sensitive Perl regular expression replace in files
                     // in all files of specified directory searching with expression
                     // (?:SP|ER)(_Dev.+)(?:SP|ER)(_Dev.+\b(?:ID1|ID2|...|IDn)_?\b)
                     // and using as replace expression ER\1ER\2_ID to replace both
                     // SP_Dev by ER_Dev and append ID to the found identifier string.
                     // So ER_Dev instead of SP_Dev can be already present in the file
                     // and the identifier string can be without or with an underscore
                     // at end. Lines containing already an identifier with _ID at end
                     // are not modified by this Perl regular expression replace in files.
                     UltraEdit.frInFiles.replace("(?:SP|ER)(_Dev.+)(?:SP|ER)(_Dev.+\\b(?:"+sAllIDs+"))_?\\b","ER\\1ER\\2_ID");
                  }
            
                  if(asAddr)  // Was at least one ID with an address found?
                  {
                     UltraEdit.frInFiles.matchCase=false;
                     // Replace in all files all found addresses by corresponding identifier.
                     for (var nAddr = 0; nAddr < asAddr.length; nAddr++)
                     {
                        // Get ID from string from file and append _ID.
                        var sID = asAddr[nAddr].replace(/^{[\t ]+"(.+?)".*$/,"$1_ID");
                        // Get ID from string from file and put \b around to find only entire addresses.
                        var sAddr = asAddr[nAddr].replace(/^{[\t ]+".+?"[\t ,]+(0x[0-9A-Fa-f]+)$/,"\\b$1\\b");
                        // Run the case-insensitive Perl regular expression replace
                        // in files for this pair of address and identifier string.
                        UltraEdit.frInFiles.replace(sAddr,sID);
                     }
                  }
               }
            }
            
            It is expected by the script that the number of identifier strings is not too large because the length of a Perl regular expression search string is not unlimited and so will not work if the active file contains too many identifiers.
            Best regards from an UC/UE/UES for Windows user from Austria

            2
            NewbieNewbie
            2

              Re: Another variation of find and replace from a data file

              Apr 01, 2019#6

              You are the man !🙂