Tapatalk

How to remove string from an opened file using a second file as reference?

How to remove string from an opened file using a second file as reference?

81
Advanced UserAdvanced User
81

    Aug 01, 2017#1

    I have a file opened in my UE, say a.txt which looks like below:

    Code: Select all

    <city>Alabama</city>
    <city>Orillia</city>
    <city>Goa</city>
    <city>Quebec</city>
    <city>Paris</city>
    <city>Alberta</city>
    <city>Victoria</city>
    <city>Queensland</city>
    <city>Assam</city>
    <city>Iowa</city>
    <city>New South Wales</city>
    And I have another reference file "cities.txt" which looks like:

    Code: Select all

    <city>Seattle</city>
    <city>Orillia</city>
    <city>Goa</city>
    <city>Paris</city>
    <city>London</city>
    <city>Bangkok</city>
    <city>Delhi</city>
    <city>Habi</city>
    <city>Mumbai</city>
    <city>Berkeley</city>
    <city>Doha</city>
    <city>Woodstock</city>
    <city>Mesa</city>
    I'm trying a create a script which on calling will open the "cities.txt" and remove any matching data from the current file using the "cities.txt" as a reference. So after the script runs, the a.txt will look like:

    Code: Select all

    <city>Alabama</city>
    <city>Quebec</city>
    <city>Alberta</city>
    <city>Victoria</city>
    <city>Queensland</city>
    <city>Assam</city>
    <city>Iowa</city>
    <city>New South Wales</city>
    This is what I've tried so far. It is not working.

    Code: Select all

    UltraEdit.open("D:\\cities.txt");
    UltraEdit.insertMode();
    if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
    else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
    UltraEdit.activeDocument.hexOff();
    UltraEdit.activeDocument.top();
    UltraEdit.activeDocument.selectLine();
    UltraEdit.selectClipboard(0);
    UltraEdit.activeDocument.copy();
    var clip = UltraEdit.clipboardContent;
    UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
    UltraEdit.activeDocument.top();
    UltraEdit.activeDocument.findReplace.mode=0;
    UltraEdit.activeDocument.findReplace.matchCase=false;
    UltraEdit.activeDocument.findReplace.matchWord=false;
    UltraEdit.activeDocument.findReplace.regExp=true;
    UltraEdit.activeDocument.findReplace.searchDown=true;
    UltraEdit.activeDocument.findReplace.searchInColumn=false;
    UltraEdit.activeDocument.findReplace.preserveCase=false;
    UltraEdit.activeDocument.findReplace.replaceAll=true;
    UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
    UltraEdit.activeDocument.findReplace.replace("clip", "");
    Can anyone help :(

    6,685587
    Grand MasterGrand Master
    6,685587

      Aug 01, 2017#2

      You have asked a similar question already once in the past, see Replace XML tags in first file with data from a second file.

      There is also One find and multiple replace from a data file and many other posted scripts for this often asked task.

      Please first search before creating a new topic. You should have really thought this time that such a question was already asked before by somebody. There are even UltraEdit macros from your 2004 (first year of the current forum database) available for this task at a time UltraEdit did not have a script feature at all.

      Code: Select all

      UltraEdit.activeDocument.findReplace.replace("clip", "");
      This line searches in file for the string clip for removing it. That is definitely not what you want.

      Code: Select all

      UltraEdit.activeDocument.findReplace.replace(clip, "");
      This line would search for value of string variable clip for removing this string value from file. That would also not work as it is unlikely that the entire block copied before to clipboard and loaded from clipboard to a string variable is found at once in the file.
      Best regards from an UC/UE/UES for Windows user from Austria

      81
      Advanced UserAdvanced User
      81

        Aug 02, 2017#3

        Sorry, :cry:
        I totally forgot that I have a similar problem before and used a macro for it.
        Anyway, a script should be much faster for a larger file.

          Aug 07, 2017#4

          Looking at my old post I've created the below macro for the above mentioned task which seems to work fine on the samples:

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          Loop 0
          UltraEditReOn
          Find RegExp "<city>*</city>"
          Copy
          NextWindow
          Key Ctrl+HOME
          UltraEditReOn
          Find RegExp "^c"
          IfNotFound
          PreviousWindow
          Key DOWN ARROW
          Key HOME
          EndIf
          IfFound
          PreviousWindow
          UltraEditReOn
          Find RegExp "^c"
          Replace All ""
          Key DEL
          EndIf
          IfEof
          ExitLoop
          EndIf
          EndLoop
          However, in a.txt file I need to add an extra blank line at the end of the file otherwise it just keeps on running.
          How do I solve that?

          6,685587
          Grand MasterGrand Master
          6,685587

            Aug 08, 2017#5

            Many of the macros posted by me contain the sequence:

            Code: Select all

            Bottom
            IfColNumGt 1
            InsertLine
            IfColNumGt 1
            DeleteToStartofLine
            EndIf
            EndIf
            Top
            This code sequence can be found also in my macro reference file which can be downloaded from sticky macros forum topic Macro examples and reference for beginners and experts.

            This macro code makes sure that the file does not end with a string, but with a line which means the file ends with a line termination. In your case it would be necessary to run this code sequence once on a.txt and on cities.txt before starting the replaces.

            don_bradman wrote:I'm trying a create a script which on calling will open the "cities.txt" and remove any matching data from the current file using the "cities.txt" as a reference.
            This explanation means on using the examples search first for <city>Seattle</city> from cities.txt in a.txt and remove it when really found. Next search for <city>Orillia</city> from from cities.txt in a.txt and remove all found occurrences. And so on until all lines from cities.txt are used on a.txt. Macros and scripts for that task are posted at lot as I wrote already once.

            The result would be for a.txt:

            Code: Select all

            <city>Alabama</city>
            <city>Quebec</city>
            <city>Alberta</city>
            <city>Victoria</city>
            <city>Queensland</city>
            <city>Assam</city>
            <city>Iowa</city>
            <city>New South Wales</city>
            This output can be get with the following script:

            Code: Select all

            // This script as coded requires at least UltraEdit for Windows v16.00
            // or UEStudio v10.00. Former versions are not supported without code
            // modification because of activeDocumentIdx and currentColumnNum usage.
            
            // Name of the cities list file with full path defined as global constant.
            var g_sCitiesFileName = "D:\\cities.txt";
            
            
            // Global variables for document index of data file to modify as well
            // as an array with line and column number inside cities list file in
            // case of being already opened on script start.
            var g_nDataFile;
            var g_anListCaretPos = [0,0];
            
            
            // Function for finding cities list file in opened files or for opening it.
            // The cities list file is the active file on successfully finding/opening it.
            function OpenListFile ()
            {
               // The active file is by default the data file to modify.
               g_nDataFile = UltraEdit.activeDocumentIdx;
            
               // First find out if the list file is opened already in UltraEdit.
               var sLowerCasesCitiesFileName = g_sCitiesFileName.toLowerCase();
               for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
               {
                   // Is the list file opened already in UltraEdit?
                  if (UltraEdit.document[nDocIndex].path.toLowerCase() == sLowerCasesCitiesFileName)
                  {
                     var nListFile = nDocIndex;
                     g_anListCaretPos[0] = UltraEdit.document[nDocIndex].currentLineNum;
                     g_anListCaretPos[1] = UltraEdit.document[nDocIndex].currentColumnNum;
            
                     // Is the active file the list file instead of the data file?
                     if (nListFile == g_nDataFile)
                     {
                        if (UltraEdit.document.length != 2)
                        {
                           UltraEdit.messageBox("Active file is the cities list file and there are more than 2 files opened.\n\nPlease make a different file active before running the script again.");
                           return false;
                        }
                        g_nDataFile = (nListFile == 0) ? 1 : 0;
                     }
                     else  // Make the list file the active file to avoid display
                     {     // refreshes on executing later the replaces when the
                           // document windows are displayed maximized.
                        UltraEdit.document[nListFile].setActive();
                     }
                     return true;
                  }
               }
            
               // The list file must be opened now and will be appended to document array.
               // So its document index will be the current number of opened files and
               // the number of opened files increases on successfully openening the file.
               nListFile = UltraEdit.document.length;
               UltraEdit.open(g_sCitiesFileName);
            
               // Could the list file with the cities be opened successfully?
               if (UltraEdit.document.length > nListFile)
               {
                  return true;
               }
            
               UltraEdit.messageBox("Failed to open file: " + g_sCitiesFileName);
               return false;
            }
            
            // Main function for reading the list file and running the replaces.
            if (UltraEdit.document.length > 0)  // Is any file opened?
            {
               if (OpenListFile())
               {
                  // Define environment for this script.
                  UltraEdit.insertMode();
                  if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                  else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            
                  // Load all lines in active cities list file into an array of strings.
                  UltraEdit.activeDocument.trimTrailingSpaces();
                  UltraEdit.activeDocument.selectAll();
                  if (UltraEdit.activeDocument.isSel())
                  {
                     var sLineTerm;
                     if (UltraEdit.activeDocument.lineTerminator <= 0) sLineTerm = "\r\n";
                     else if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
                     else sLineTerm = "\r";
                     var asCities = UltraEdit.activeDocument.selection.split(sLineTerm);
            
                     // Make sure the last line of the data file has also a line termination.
                     UltraEdit.document[g_nDataFile].bottom();
                     if (UltraEdit.document[g_nDataFile].isColNumGt(1))
                     {
                        UltraEdit.document[g_nDataFile].insertLine();
                        if (UltraEdit.document[g_nDataFile].isColNumGt(1))
                        {
                           UltraEdit.document[g_nDataFile].deleteToStartOfLine();
                        }
                     }
            
                     // Determine the line termination type of active file.
                     if (UltraEdit.document[g_nDataFile].lineTerminator <= 0) sLineTerm = "\r\n";
                     else if (UltraEdit.document[g_nDataFile].lineTerminator == 1) sLineTerm = "\n";
                     else sLineTerm = "\r";
            
                     // Move caret to top of data file to modify and define the
                     // parameters for the replaces executed in the FOR loop below.
                     UltraEdit.document[g_nDataFile].top();
                     UltraEdit.ueReOn();
                     UltraEdit.document[g_nDataFile].findReplace.mode=0;
                     UltraEdit.document[g_nDataFile].findReplace.matchCase=false;
                     UltraEdit.document[g_nDataFile].findReplace.matchWord=false;
                     UltraEdit.document[g_nDataFile].findReplace.regExp=true;
                     UltraEdit.document[g_nDataFile].findReplace.searchDown=true;
                     UltraEdit.document[g_nDataFile].findReplace.searchInColumn=false;
                     UltraEdit.document[g_nDataFile].findReplace.preserveCase=false;
                     UltraEdit.document[g_nDataFile].findReplace.replaceAll=true;
                     UltraEdit.document[g_nDataFile].findReplace.replaceInAllOpen=false;
                     var bInvalidLines = false;
            
                     for (var nCity = 0; nCity < asCities.length; nCity++)
                     {
                        // Ignore empty lines in cities list file.
                        if (!asCities[nCity].length) continue;
                        // Process only lines from list file matching the following
                        // pattern and log all other invalid lines in output window.
                        if (asCities[nCity].search(/^[\t ]*<city>.+?<\/city>$/) >= 0)
                        {
                           var sSearchExp = "%[^t ]++";
                           sSearchExp += asCities[nCity].replace(/^[\t ]*(<city>.+?<\/city>)$/,"$1");
                           sSearchExp += "[^t ]++" + sLineTerm;
                           UltraEdit.document[g_nDataFile].findReplace.replace(sSearchExp,"");
                        }
                        else
                        {
                           if (!bInvalidLines)
                           {
                              UltraEdit.outputWindow.clear();
                              UltraEdit.outputWindow.write("1 or more invalid lines found in cities list file:\n");
                              bInvalidLines = true;
                           }
                           UltraEdit.outputWindow.write(g_sCitiesFileName + "(" + (nCity+1).toString(10) + "): " + asCities[nCity]);
                        }
                     }
            
                     // Was the cities list file opened by the script?
                     if (g_anListCaretPos[0] == 0)
                     {
                        // Make the data file the active file and close list file.
                        var nListFile = UltraEdit.activeDocumentIdx;
                        UltraEdit.document[g_nDataFile].setActive();
                        UltraEdit.closeFile(UltraEdit.document[nListFile].path,2);
                     }
                     else
                     {
                        // Restore initial position in list file
                        // and make the data file the active file.
                        UltraEdit.activeDocument.gotoLine(g_anListCaretPos[0],g_anListCaretPos[1]);
                        UltraEdit.document[g_nDataFile].setActive();
                     }
            
                     // Was at least 1 invalid line written to output window?
                     if (bInvalidLines)
                     {
                        // Make the output window visible and disable
                        // printing status information into output window.
                        if (UltraEdit.outputWindow.visible == false)
                        {
                           UltraEdit.outputWindow.showWindow(true);
                        }
                        UltraEdit.outputWindow.showStatus=false;
                     }
                  }
               }
            }

            After having finished the script above I compared it with the output example and could see that the expected output is the opposite. Instead of removing from a.txt all cities listed in cities.txt, the expected output should be that a.txt contains finally only cities listed in cities.txt.

            So I modified the script to:

            Code: Select all

            // This script as coded requires at least UltraEdit for Windows v16.00
            // or UEStudio v10.00. Former versions are not supported without code
            // modification because of activeDocumentIdx and currentColumnNum usage.
            
            // Name of the cities list file with full path defined as global constant.
            var g_sCitiesFileName = "D:\\cities.txt";
            
            
            // Global variables for document index of data file to modify as well
            // as an array with line and column number inside cities list file in
            // case of being already opened on script start.
            var g_nDataFile;
            var g_anListCaretPos = [0,0];
            
            
            // Function for finding cities list file in opened files or for opening it.
            // The cities list file is the active file on successfully finding/opening it.
            function OpenListFile ()
            {
               // The active file is by default the data file to modify.
               g_nDataFile = UltraEdit.activeDocumentIdx;
            
               // First find out if the list file is opened already in UltraEdit.
               var sLowerCasesCitiesFileName = g_sCitiesFileName.toLowerCase();
               for (var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
               {
                   // Is the list file opened already in UltraEdit?
                  if (UltraEdit.document[nDocIndex].path.toLowerCase() == sLowerCasesCitiesFileName)
                  {
                     var nListFile = nDocIndex;
                     g_anListCaretPos[0] = UltraEdit.document[nDocIndex].currentLineNum;
                     g_anListCaretPos[1] = UltraEdit.document[nDocIndex].currentColumnNum;
            
                     // Is the active file the list file instead of the data file?
                     if (nListFile == g_nDataFile)
                     {
                        if (UltraEdit.document.length != 2)
                        {
                           UltraEdit.messageBox("Active file is the cities list file and there are more than 2 files opened.\n\nPlease make a different file active before running the script again.");
                           return false;
                        }
                        g_nDataFile = (nListFile == 0) ? 1 : 0;
                     }
                     else  // Make the list file the active file to avoid display
                     {     // refreshes on executing later the replaces when the
                           // document windows are displayed maximized.
                        UltraEdit.document[nListFile].setActive();
                     }
                     return true;
                  }
               }
            
               // The list file must be opened now and will be appended to document array.
               // So its document index will be the current number of opened files and
               // the number of opened files increases on successfully openening the file.
               nListFile = UltraEdit.document.length;
               UltraEdit.open(g_sCitiesFileName);
            
               // Could the list file with the cities be opened successfully?
               if (UltraEdit.document.length > nListFile)
               {
                  return true;
               }
            
               UltraEdit.messageBox("Failed to open file: " + g_sCitiesFileName);
               return false;
            }
            
            // Main function for reading the list file and running the replaces.
            if (UltraEdit.document.length > 0)  // Is any file opened?
            {
               if (OpenListFile())
               {
                  // Define environment for this script.
                  UltraEdit.insertMode();
                  if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                  else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            
                  // Load all lines in active cities list file into an array of strings.
                  UltraEdit.activeDocument.trimTrailingSpaces();
                  UltraEdit.activeDocument.selectAll();
                  if (UltraEdit.activeDocument.isSel())
                  {
                     var sLineTerm;
                     if (UltraEdit.activeDocument.lineTerminator <= 0) sLineTerm = "\r\n";
                     else if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
                     else sLineTerm = "\r";
                     var asCities = UltraEdit.activeDocument.selection.split(sLineTerm);
            
                     // Make sure the last line of the data file has also a line termination.
                     UltraEdit.document[g_nDataFile].bottom();
                     if (UltraEdit.document[g_nDataFile].isColNumGt(1))
                     {
                        UltraEdit.document[g_nDataFile].insertLine();
                        if (UltraEdit.document[g_nDataFile].isColNumGt(1))
                        {
                           UltraEdit.document[g_nDataFile].deleteToStartOfLine();
                        }
                     }
            
                     // Determine the line termination type of active file.
                     if (UltraEdit.document[g_nDataFile].lineTerminator <= 0) sLineTerm = "\r\n";
                     else if (UltraEdit.document[g_nDataFile].lineTerminator == 1) sLineTerm = "\n";
                     else sLineTerm = "\r";
            
                     // Move caret to top of data file to modify and define the
                     // parameters for the replaces executed in the FOR loop below.
                     UltraEdit.document[g_nDataFile].top();
                     UltraEdit.ueReOn();
                     UltraEdit.document[g_nDataFile].findReplace.mode=0;
                     UltraEdit.document[g_nDataFile].findReplace.matchCase=false;
                     UltraEdit.document[g_nDataFile].findReplace.matchWord=false;
                     UltraEdit.document[g_nDataFile].findReplace.regExp=true;
                     UltraEdit.document[g_nDataFile].findReplace.searchDown=true;
                     UltraEdit.document[g_nDataFile].findReplace.searchInColumn=false;
                     UltraEdit.document[g_nDataFile].findReplace.preserveCase=false;
                     UltraEdit.document[g_nDataFile].findReplace.replaceAll=true;
                     UltraEdit.document[g_nDataFile].findReplace.replaceInAllOpen=false;
                     var bInvalidLines = false;
            
                     for (var nCity = 0; nCity < asCities.length; nCity++)
                     {
                        // Ignore empty lines in cities list file.
                        if (!asCities[nCity].length) continue;
                        // Process only lines from list file matching the following
                        // pattern and log all other invalid lines in output window.
                        if (asCities[nCity].search(/^[\t ]*<city>.+?<\/city>$/) >= 0)
                        {
                           var sSearchExp = "%^([^t ]++";
                           sSearchExp += asCities[nCity].replace(/^[\t ]*(<city>.+?<\/city>)$/,"$1");
                           sSearchExp += "^)";
                           UltraEdit.document[g_nDataFile].findReplace.replace(sSearchExp,"#^1");
                        }
                        else
                        {
                           if (!bInvalidLines)
                           {
                              UltraEdit.outputWindow.clear();
                              UltraEdit.outputWindow.write("1 or more invalid lines found in cities list file:\n");
                              bInvalidLines = true;
                           }
                           UltraEdit.outputWindow.write(g_sCitiesFileName + "(" + (nCity+1).toString(10) + "): " + asCities[nCity]);
                        }
                     }
            
                     // Remove all lines not marked with # at beginning of the line.
                     UltraEdit.document[g_nDataFile].findReplace.replace("%[~#^r^n]*"+sLineTerm,"");
                     // Remove the marker character # from beginning of the remaining lines.
                     UltraEdit.document[g_nDataFile].findReplace.replace("%#","");
                     
                     // Was the cities list file opened by the script?
                     if (g_anListCaretPos[0] == 0)
                     {
                        // Make the data file the active file and close list file.
                        var nListFile = UltraEdit.activeDocumentIdx;
                        UltraEdit.document[g_nDataFile].setActive();
                        UltraEdit.closeFile(UltraEdit.document[nListFile].path,2);
                     }
                     else
                     {
                        // Restore initial position in list file
                        // and make the data file the active file.
                        UltraEdit.activeDocument.gotoLine(g_anListCaretPos[0],g_anListCaretPos[1]);
                        UltraEdit.document[g_nDataFile].setActive();
                     }
            
                     // Was at least 1 invalid line written to output window?
                     if (bInvalidLines)
                     {
                        // Make the output window visible and disable
                        // printing status information into output window.
                        if (UltraEdit.outputWindow.visible == false)
                        {
                           UltraEdit.outputWindow.showWindow(true);
                        }
                        UltraEdit.outputWindow.showStatus=false;
                     }
                  }
               }
            }
            This script modifies a.txt to:

            Code: Select all

            <city>Orillia</city>
            <city>Goa</city>
            <city>Paris</city>
            That result is also different to posted expected output because of last line <city>Iowa</city>, but this is most likely a mistake made on creating the output example block.

            As I know that people from India makes everything wrong which can be made wrong (learned over years here in forum and also at work with our colleagues from India offices), I added lots of extra code which would be in general not necessary to make the scripts as error proof as possible.

            It took me 3 hours to write both scripts, test them and write this reply. I hope I don't need to do anything else because you find out now with having the scripts that you want something different which would be also typical for people from India.
            Best regards from an UC/UE/UES for Windows user from Austria

            81
            Advanced UserAdvanced User
            81

              Aug 08, 2017#6

              The desired output should be:

              Code: Select all

              <city>Alabama</city>
              <city>Quebec</city>
              <city>Alberta</city>
              <city>Victoria</city>
              <city>Queensland</city>
              <city>Assam</city>
              <city>Iowa</city>
              <city>New South Wales</city>
              I messed it up while writing this post as I was in a hurry. Even the macro that I posted yesterday generated the above output.
              It was an unintentional mistake. Sorry for that.