Compare two files line by line and get not matching lines into two new files

Compare two files line by line and get not matching lines into two new files

2

    Oct 14, 2013#1

    I have 2 ASCII files I wish to compare with UltraCompare. Each contains N lines of 6 characters each. I want to find lines in file A not in file B and lines in file B not in file A. This is all or nothing. I don't want the "First Different, Second Different" report.

    I have tried changing the session properties with no success.

    Advice appreciated.

    Thanks,

    ~john

    6,687586
    Grand MasterGrand Master
    6,687586

      Oct 15, 2013#2

      Such tasks are better done with an UltraEdit script or macro. There are several scripts/macros available for very similar tasks.

      I wrote nevertheless very quickly a new script exactly for this task. It does not contain any error handling like one file is empty. But I think, you don't need them.

      Open in UltraEdit the two files. Create a new ASCII file with DOS line terminators and copy & paste the following script code into this new file. Save the new file with a name like GetUniqueLinesFromTwoFiles.js. Run the script via Scripting - Run Active Script.

      Code: Select all

      if (UltraEdit.document.length > 1)  // Are at least 2 files opened?
      {
         // Define environment for the script.
         UltraEdit.insertMode();
         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.
         UltraEdit.document[0].selectAll();
         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.
         UltraEdit.document[1].selectAll();
         var asLinesFileB = UltraEdit.document[1].selection.split("\r\n");
         if (asLinesFileB[asLinesFileB.length-1] == "") asLinesFileB.pop();
      
         // Remove the lines existing in both files.
         for (var nLineA = 0; nLineA < asLinesFileA.length; nLineA++)
         {
            var sLineA = asLinesFileA[nLineA];
            for (var nLineB = 0; nLineB < asLinesFileB.length; nLineB++)
            {
               if (asLinesFileB[nLineB] == sLineA)
               {
                  asLinesFileB.splice(nLineB,1);
                  asLinesFileA.splice(nLineA,1);
                  nLineA--;
                  break;
               }
            }
         }
         UltraEdit.selectClipboard(9);
         // Are unique lines from file A remaining, write them to a new file.
         if (asLinesFileA.length > 0)
         {
            UltraEdit.newFile();
            UltraEdit.activeDocument.write("Unique in file "+UltraEdit.document[0].path+":\r\n\r\n\r\n");
            UltraEdit.activeDocument.key("UP ARROW");
            UltraEdit.clipboardContent = asLinesFileA.join("\r\n");
            UltraEdit.activeDocument.paste();
            UltraEdit.activeDocument.top();
         }
         // Are unique lines from file B remaining, write them to a new file.
         if (asLinesFileB.length > 0)
         {
            UltraEdit.newFile();
            UltraEdit.activeDocument.write("Unique in file "+UltraEdit.document[1].path+":\r\n\r\n\r\n");
            UltraEdit.activeDocument.key("UP ARROW");
            UltraEdit.clipboardContent = asLinesFileB.join("\r\n");
            UltraEdit.activeDocument.paste();
            UltraEdit.activeDocument.top();
         }
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      2

        Oct 15, 2013#3

        Mofi,

        Thanks. That was kind of you. I ran the script and the results are as you say.

        I will learn about scripting.

        Regards,

        ~john

        2
        NewbieNewbie
        2

          Report strings in file A not found in file B into a new file

          Nov 03, 2013#4

          I'd like to ask your help on the following requirements.
          There are search strings in file A to search for in file B.
          If the search string is not found in file B, we need to list the search string in a mismatch report.
          A search string is followed by two ampersands in file A. A search string is to be found as a full word, not partial word in file B.
          Thank you in advance.
          Example below.

          File A

          Code: Select all

          &&apple
                   &&orange
             &&desk
                      &&fruit
            &&test
                
          &&spring
          File B

          Code: Select all

               test
            orangejuice
                   desk
          Mismatch report

          Code: Select all

          apple
          orange
          fruit
          spring

          6,687586
          Grand MasterGrand Master
          6,687586

            Re: Report strings in file A not found in file B into a new file

            Nov 03, 2013#5

            Here is the script searching in file B for strings defined in file A and reporting all strings from file A not found in file B.

            Code: Select all

            if (UltraEdit.document.length > 1)  // Are at least 2 files opened?
            {
               // Define environment for the script.
               UltraEdit.insertMode();
               if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
               else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            
               // Get all strings starting with 2 ampersands.
               UltraEdit.document[0].selectAll();
               var asSearchStrings = UltraEdit.document[0].selection.match(/&&\S+/g);
               UltraEdit.document[0].top();  // Cancel the selection.
            
               if (asSearchStrings != null)  // Was any string found?
               {
                  // Get content of second file into a large string.
                  UltraEdit.document[1].selectAll();
                  var asContentFileB = UltraEdit.document[1].selection;
                  UltraEdit.document[1].top();
            
                  // Remove the 2 ampersands from each found string and
                  // search for each string in entire content of file B.
                  var nIndex = asSearchStrings.length;
                  do
                  {
                     var sToFind = asSearchStrings[--nIndex].substr(2);
                     var rSearch = new RegExp("\\b"+sToFind+"\\b","");
                     // Can this string as a word found in file B?
                     if (asContentFileB.search(rSearch) < 0)
                     {  // No, keep this string for final report.
                        asSearchStrings[nIndex] = sToFind;
                     }
                     else
                     {  // Yes, remove this string from the array of search strings.
                        asSearchStrings.splice(nIndex,1);
                     }
                  }
                  while (nIndex > 0);
            
                  // Are search strings from file A remaining, write them to a new file.
                  if (asSearchStrings.length > 0)
                  {
                     asSearchStrings.push("");  // To terminate last line, too.
                     UltraEdit.selectClipboard(9);
                     UltraEdit.newFile();
                     UltraEdit.activeDocument.unixMacToDos();
                     UltraEdit.clipboardContent = asSearchStrings.join("\r\n");
                     UltraEdit.activeDocument.paste();
                     UltraEdit.activeDocument.top();
                     UltraEdit.clearClipboard();
                     UltraEdit.selectClipboard(0);
                  }
                  else UltraEdit.messageBox("All strings from file A found in file B.");
               }
            }
            Best regards from an UC/UE/UES for Windows user from Austria

            2
            NewbieNewbie
            2

              Re: Report strings in file A not found in file B into a new file

              Nov 03, 2013#6

              Thank you much Mofi, it works great.
              Bless you.