Tapatalk

One find and multiple replace from a data file

One find and multiple replace from a data file

1
NewbieNewbie
1

    Apr 19, 2012#1

    Example :

    1) I have a text file with 1000 same words (let the word be "friends").

    2) I have another text file like

    fowl
    frame
    free
    frequent
    friend
    ......
    ......
    so on.

    3) Now need to replace the friends of 1st text file with the 2nd text file like

    friends ......fowl
    friends ......frame
    friends ......free
    friends ......frequent
    friends ......friend
    .....
    .....
    So on.

    1st friends should be replaced with fowl
    2nd friends should be replaced with frame
    3rd friends should be replaced with free
    4th friends should be replaced with frequent
    5th friends should be replaced with friend

    Need a macro or script for this task.

    Sorry for my English..hope I'm clear :( :(

    6,685587
    Grand MasterGrand Master
    6,685587

      Apr 19, 2012#2

      Copy this script into a new ASCII/ANSI file with DOS line terminators, save it, close it and add the script to the list of scripts in menu Scripting. Open the two files, make the file containing the placeholder string friends the active file and run the script from menu Scripting.

      Code: Select all

      if (UltraEdit.document.length > 1) // At least 2 files must be open.
      {
         // Define the environment for the script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
      
         // The active file is the file to modify with the strings stored
         // line by line in the other opened file which must be the list 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");
            // Remove last string if it is an empty string because list file ended with a line termination.
            if (asStrings[asStrings.length-1] == "") asStrings.pop();
            // Discard the selection in the list file by moving caret to top of file.
            UltraEdit.document[nListFile].top();
      
            // Now the placeholder strings in active file can be replaced by strings from list file.
            UltraEdit.ueReOn();
            UltraEdit.activeDocument.findReplace.mode=0;
            UltraEdit.activeDocument.findReplace.matchCase=false;
            UltraEdit.activeDocument.findReplace.matchWord=false;
            UltraEdit.activeDocument.findReplace.regExp=false;
            UltraEdit.activeDocument.findReplace.searchDown=true;
            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
            // or up to last found occurrence of the placeholder string.
            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;
               if (!UltraEdit.activeDocument.findReplace.find("friends")) break;
               UltraEdit.activeDocument.write(asStrings[nStringIndex]);
            }
         }
      }