Search and Delete Line Based on Value in Certain Columns

Search and Delete Line Based on Value in Certain Columns

3

    Dec 24, 2020#1

    Long time lurker, but could not find an answer to my problem for once (unless I did not look hard enough). I am attempting to create a script that will search for a specific value only in certain columns, and once found, delete that line of text.
    So far I have the below (pretty sure I pulled most of this from Mofi):

    Code: Select all

    if (UltraEdit.document.length) {
      UltraEdit.insertMode();
      if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
      else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      UltraEdit.perlReOn();
      UltraEdit.activeDocument.top();
      var sUserString = UltraEdit.getString("What is the first node position to be deleted??",1);
      var sDelString = sUserString.replace(/([$()*+.?\[\]\\{|}\^])/g,"\\$1");
      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;
      var sSearchString = "^.*" + sDelString + ".*(?:\\r?\\n|\\r)";
      UltraEdit.activeDocument.findReplace.replace(sSearchString,"");
    }
    This will remove any line of data that contains the input value, but I need to confine the search to only specific columns, not the entire file.
    Adding to that, once the line that contained the specific value has been deleted, I need to have every other line (so skip a line, delete a line, skip a line, etc.) after that deleted as well.
    Hope this makes sense. Any input would be greatly appreciated. Many thanks.

    UltraEdit Professional Text/HEX Editor
    Version 22.10.0.10
    Windows 10, 64-Bit

    6,602548
    Grand MasterGrand Master
    6,602548

      Dec 25, 2020#2

      Is this extended script suitable for the line deletion task?

      Code: Select all

      if (UltraEdit.document.length) {
        UltraEdit.insertMode();
        if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
        else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
        UltraEdit.perlReOn();
        UltraEdit.activeDocument.top();
        var nStartColumn = 0;
        var sUserString = "";
        while (!sUserString.length) sUserString = UltraEdit.getString("Which string should be found to delete the line?",1);
        while (nStartColumn < 1) nStartColumn = UltraEdit.getValue("At which column should the string be found to delete the line?",1);
        var sDelString = sUserString.replace(/([$()*+.?\[\]\\{|}\^])/g,"\\$1");
        var sCharCount = (nStartColumn != 1) ? ".{" + (nStartColumn-1).toString(10) + "}" : "";
        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;
        var sSearchString = "^" + sCharCount + sDelString + ".*(?:\\r?\\n|\\r)";
        UltraEdit.activeDocument.findReplace.replace(sSearchString,"");
      }
      
      Note: Each horizontal tab character is counted as one character. So column number is in real character number in line.
      Best regards from an UC/UE/UES for Windows user from Austria

      3

        Dec 26, 2020#3

        Mofi, 
        Yes, after some testing on several different files, this script works perfectly for my intended purposes. Many thanks.
        Any chance you have some input on how to make it so that after the target line has been deleted, make it delete every other line after it?
        So if I tell it to delete value 1329 in a specific column, it will delete every other line following?

        Sample: Numbers edited for work purposes.

        H    1100.0      1325  1A1                     123456.7  891011.1   2.1314151671.819202
        H    1100.0      1327  1A1                     123456.7  891011.1   2.1314151671.819202
        H    1100.0      1329  1A1                     123456.7  891011.1   2.1314151671.819202  <first line to be deleted. Handled by script you already developed.
        H    1100.0      1331  1A1                      123456.7  891011.1   2.1314151671.819202
        H    1100.0      1333  1A1                     123456.7  891011.1   2.1314151671.819202  <every other line after target line deleted needs to be deleted as well.
        H    1100.0      1335  1A1                     123456.7  891011.1   2.1314151671.819202
        H    1100.0      1337  1A1                     123456.7  891011.1   2.1314151671.819202  <needs to be deleted.

        The files I am working with are thousands of lines long, containing unique values for each line of data. In the above sample, the values for 1325 - 1337 increment by 2. However, at a certain point (different for each file), that increment will need to change from 2 to 4. So by defining the first line to be deleted, which you have already done (many thanks by the way), the starting point for the line deletion is defined, and now every other line after that needs to be deleted. I hope this makes sense somewhat. Apologies if I am being unclear. Many thanks again for the input.

        6,602548
        Grand MasterGrand Master
        6,602548

          Dec 27, 2020#4

          I hope, this script is what you need for your task.

          Code: Select all

          if (UltraEdit.document.length) {
            UltraEdit.insertMode();
            if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
            else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            UltraEdit.perlReOn();
            UltraEdit.activeDocument.top();
            var nStartColumn = 0;
            var sUserString = "";
            while (!sUserString.length) sUserString = UltraEdit.getString("Which string should be found to delete the line?",1);
            while (nStartColumn < 1) nStartColumn = UltraEdit.getValue("At which column should the string be found to delete the line?",1);
            nStartColumn--;
            var sDelString = sUserString.replace(/([$()*+.?\[\]\\{|}\^])/g,"\\$1");
            var sStartExp = (nStartColumn != 1) ? "^.{" + nStartColumn.toString(10) + "}" : "^";
            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=false;
            UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
            var sSearchString = sStartExp + sDelString + ".*(?:\\r?\\n|\\r|$)";
            if (UltraEdit.activeDocument.findReplace.find(sSearchString))
            {
              // Get the selected line.
              var sFoundLine = UltraEdit.activeDocument.selection;
              // Get the string from start column to end of line.
              var sLineEnd = sFoundLine.substr(nStartColumn);
              // Get the number at start column if there is a number at all.
              var sNumber = sLineEnd.replace(/^(\d+)[\s\S]*$/,"$1");
              // Is there really a number at the start column?
              if (sNumber.length != sLineEnd.length)
              {
                // Prompt the script user for the increment while selected line visible.
                var nIncrement = 0;
                while (nIncrement < 1) nIncrement = UltraEdit.getValue("Which increment to use for " + sNumber + "?",1);
                var nNumber = parseInt(sNumber,10);
                // Delete the selected line.
                UltraEdit.activeDocument.deleteText();
                // Delete all the following lines with a number at start column
                // incremented from previous number by the value entered by the
                // script user until there is no line found with searched number.
                do
                {
                  nNumber += nIncrement;
                  sSearchString = sStartExp + nNumber.toString(10) + "\\b.*(?:\\r?\\n|\\r|$)";
                }
                while (UltraEdit.activeDocument.findReplace.replace(sSearchString,""));
              }
            }
          }
          
          Best regards from an UC/UE/UES for Windows user from Austria

          3

            Dec 28, 2020#5

            Mofi, this is perfect. Thanks so much for all your help with this. I greatly appreciate it.