Tapatalk

Side by side compare in one file

Side by side compare in one file

61
Advanced UserAdvanced User
61

    Feb 07, 2013#1

    I have a one file and it looks like this.
    |OldCname|NewCname|OldADD|NewADD|OldPhone|newPhone|
    |A|A|123|124|5551212|55151212|

    --This is an example. The first row has the names of the columns.
    -- all rows after have data. All COLUMNS are seperated by the same seperation character. here it is the -> |
    if you count the (( # of '|' )-1)/2 you get 3 which is how many compares for each row.
    I want to compare each pair. The columns are of varying length. This is why I am using the seperator ( old|new) .
    I would like an an output file that spells out each difference for each line. No output for matches.
    For example in the output file.
    I would see
    Line 1: OldCname -> 123, NewCname -> 124
    Line 1: OldPhone -> 5551212, NewPhone -> 5511212
    ...... for any non matched pair on a line.


    and so on for each line that do not have a match for any pair. The number of old/new pairs can be long and is variable, but always in pair groups.
    The three variables I see are the
    Seperator, number of pairs and the output file to store the diff info.

    Any help would be appreciated.

    6,685587
    Grand MasterGrand Master
    6,685587

      Feb 08, 2013#2

      Here is the simple script which should do the task quickly.

      Code: Select all

      if (UltraEdit.document.length > 0) {   // Is any file currently opened?
      
         // Define the environment for the script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
      
         // Select entire file content.
         UltraEdit.activeDocument.selectAll();
         if (UltraEdit.activeDocument.isSel()) {  // Is active file not an empty file?
      
            // Determine line termination.
            var sLineTerm = "\r\n";
            if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
            else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";
      
            // Get all lines into an array of strings.
            var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
            UltraEdit.activeDocument.top();       // Cancel the selection.
      
            var asHeaders = asLines[0].split("|");
            if (asHeaders != null) {
               // If last header string is an empty string because the
               // line ended with |, remove this string from the array.
               if (asHeaders[asHeaders.length-1].length == 0) asHeaders.pop();
               // Remove also the first string if it is an empty
               // string because the line started with separator |.
               if (asHeaders[0].length == 0) asHeaders.shift();
      
               // Remove the last line string if it is an empty string
               // because last line of active file has a line termination.
               if (asLines[asLines.length-1].length == 0) asLines.pop();
      
               // Use clipboard 9 for output data.
               UltraEdit.selectClipboard(9);
               UltraEdit.clearClipboard();
      
               // Process all data lines.
               for (var nLineNum = 1; nLineNum < asLines.length; nLineNum++) {
      
                  // Uncomment next line if empty lines could be present in file.
                  // if (asLines[nLineNum].length == 0) continue;
      
                  // Get the | separated elements of this line.
                  var asElements = asLines[nLineNum].split("|");
      
                  // Remove first and last string if empty strings.
                  if (asElements[asElements.length-1].length == 0) asElements.pop();
                  if (asElements[0].length == 0) asElements.shift();
      
                  // Compare the pairs of elements of current line.
                  for (var nElementNum = 0; nElementNum < asElements.length; nElementNum += 2) {
      
                     // Are the two strings identical (case sensitive), continue with next pair.
                     if (asElements[nElementNum] == asElements[nElementNum+1]) continue;
      
                     // The strings are different. Append the output to user clipboard 9.
                     UltraEdit.clipboardContent += "Line "+nLineNum+": "+asHeaders[nElementNum]+" -> "+asElements[nElementNum]+", "+
                                                   asHeaders[nElementNum+1]+" -> "+asElements[nElementNum+1]+"\r\n";
                  }
               }
               // Are not identical element strings found which should be output to a file?
               if (UltraEdit.clipboardContent.length) {
                  UltraEdit.newFile();
                  // Make sure that line terminator type is DOS as the lines in clipboard.
                  UltraEdit.activeDocument.unixMacToDos();
                  UltraEdit.activeDocument.paste();
                  UltraEdit.activeDocument.top();
                  UltraEdit.clearClipboard();
               }
               else {
                  UltraEdit.messageBox("All pairs are identical!");
               }
               UltraEdit.selectClipboard(0);
            }
         }
      }

      61
      Advanced UserAdvanced User
      61

        Feb 21, 2013#3

        Thanks!!!!!!

        Works great!!!!! :D :D :D :D :D :D :D