Side by side compare in one file

Help with writing and running scripts

Side by side compare in one file

Postby sklad2 » Thu Feb 07, 2013 1:44 pm

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.
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 59
Joined: Thu Mar 08, 2007 12:00 am

Re: Side by side compare in one file

Postby Mofi » Fri Feb 08, 2013 2:05 am

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);
      }
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Side by side compare in one file

Postby sklad2 » Thu Feb 21, 2013 3:48 pm

Thanks!!!!!!

Works great!!!!! :D :D :D :D :D :D :D
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 59
Joined: Thu Mar 08, 2007 12:00 am


Return to Scripts