Determine and list starting portion of two strings that match in each line of 2 files

Determine and list starting portion of two strings that match in each line of 2 files


    Dec 12, 2013#1

    I would like to write a script that compares two files line by line. For each of the two lines, determine the portion that matches and discard the rest, then writing the matched portion to a third file.

    For example:
    File 1:
    Common Area 2B Corr Zn (G.C20)

    File 2:
    Common Area 2B Corr Zn (G0

    Copy to File 3:
    Common Area 2B Corr Zn (G

    Thanks for any assistance!

    Grand MasterGrand Master

      Dec 13, 2013#2

      This was an easy to write script by using the script posted at Compare two files line by line and get not matching lines into two new files as template.

      Open the two files to be compared which must be always first and second opened file. The files must be "small" files as completely loaded into memory limited for 32-bit applications like UltraEdit to 2 GB. And the files must have DOS line terminators because of using "\r\n" in the script.

      The file with the script code below can be opened as third file and executed with Scripting - Run Active Script or from the Scripting menu or the Script List if the script is added to the list of scripts via Scripting - Scripts.

      Code: Select all

      if (UltraEdit.document.length > 1)  // Are at least 2 files opened?
         // Define environment for the script.
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         // Get all lines from first (most left) file.
         var asLinesFileA = UltraEdit.document[0].selection.split("\r\n");
         // Remove last element from the array if it is an empty string
         // because the file ended with a DOS line termination.
         if (asLinesFileA[asLinesFileA.length-1] == "") asLinesFileA.pop();
         // Get all lines from second file.
         var asLinesFileB = UltraEdit.document[1].selection.split("\r\n");
         if (asLinesFileB[asLinesFileB.length-1] == "") asLinesFileB.pop();
         // The file with the smaller number of lines defines the number of lines to compare.
         var nLineCount = (asLinesFileA.length < asLinesFileB.length) ? asLinesFileA.length : asLinesFileB.length;
         // Create a new array for matching strings.
         var asMatches = new Array();
         // Compare the characters of each line.
         for (var nLineIndex = 0; nLineIndex < nLineCount; nLineIndex++)
            var sLineA = asLinesFileA[nLineIndex];
            var sLineB = asLinesFileB[nLineIndex];
            // The shorther line determines the number of characters to compare.
            var nCharCount = (sLineA.length < sLineB.length) ? sLineA.length : sLineB.length;
            for (var nCharIndex = 0; nCharIndex < nCharCount; nCharIndex++)
               // If current character in both lines is not equal, break the loop.
               if (sLineA[nCharIndex] != sLineB[nCharIndex]) break;
            // Are there matching characters at beginning of the line?
            if (nCharIndex > 0)
               // Copy the matching string part to the array for matching strings.
               asMatches[asMatches.length] = sLineA.substr(0,nCharIndex);
         // Are any matching strings found?
         if (asMatches.length)
            // Append an empty string to get finally the matching strings
            // with a line termination also at end of the new file.
            asMatches[asMatches.length] = "";
            // Join the matching strings with a carriage return and
            // a line-feed between each string in user clipboard 9.
            UltraEdit.clipboardContent = asMatches.join("\r\n");
            // Create a new file and make sure that the new file
            // uses DOS line terminators as in the user clipboard.
            // Paste the matching strings into the new file, move the caret
            // to top of this file, clear the user clipboard 9 and make the
            // Windows clipboard the active clipboard before exiting the script.
            asMatches.pop();  // Remove appended empty string.
         // Output a message with number of matching strings found.
         UltraEdit.messageBox("Found "+asMatches.length+" matching string"+((asMatches.length!=1)?"s.":"."));
         UltraEdit.messageBox("There must be at least 2 files opened for this script!");
      Best regards from an UC/UE/UES for Windows user from Austria