Hide or delete odd or even lines in file or every Nth line

Hide or delete odd or even lines in file or every Nth line

2
NewbieNewbie
2

    Jul 01, 2011#1

    Hi to all!
    There are a lot of topics and explanations how to find and delete specific lines by string.
    I need to delete or hide at least odd or even lines in file.
    Each line consists X Y and Z coordinates only.
    If somebody can, please write for me script or macro, or any another tool to do this job.
    If I didn't put enough information, please ask me.

    Best regards,
    Valentine

    6,675585
    Grand MasterGrand Master
    6,675585

      Jul 02, 2011#2

      For deleting all even DOS terminated lines:

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Bottom
      IfColNumGt 1
      InsertLine
      IfColNumGt 1
      DeleteToStartofLine
      EndIf
      EndIf
      Top
      Find RegExp "%^(*^)^p*$"
      Replace All "^1"


      For deleting all odd DOS terminated lines (also for files with an odd number of lines):

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Bottom
      IfColNumGt 1
      InsertLine
      IfColNumGt 1
      DeleteToStartofLine
      EndIf
      EndIf
      GotoLine 2 1
      Find RegExp "%^(*^)^p*$"
      Replace All "^1"
      Top
      DeleteLine


      The code block

      Bottom
      IfColNumGt 1
      InsertLine
      IfColNumGt 1
      DeleteToStartofLine
      EndIf
      EndIf


      verifies if there is a line termination at end of file, and if this is not the case, it appends a line termination. With auto-indent feature enabled and last line starting with spaces/tabs, the command InsertLine does not only append the missing line termination, it also appends the whitespace string at beginning of the last line of the file. The inner IfColNumGt deletes this whitespace string if also appended.

      For files with UNIX line terminators opened without temporary conversion to DOS ^p in the macros must be replace by ^n.

      For files with MAC line terminators opened without temporary conversion to DOS ^p in the macros must be replace by ^r.

      2
      NewbieNewbie
      2

        Jul 05, 2011#3

        Hi Mofi!
        You are excellent!
        I don't know how you make your expressions, but it is working!
        I wanted to understand and slightly modify it, but it is beyond my understanding!
        Can you help me again?
        I need to store each Nth row, and rest to delete.
        N may be: 5, 10, 13, etc.,.
        Can you write the common macro in, which I will be able to modify only one variable to match it to different N?

        Best regards,
        Valentine

        6,675585
        Grand MasterGrand Master
        6,675585

          Jul 06, 2011#4

          Find RegExp "%^(*^)^p*$"
          Replace All "^1"


          is an UltraEdit regular expression replace all command. The general special characters like ^p are explained in help for example on the page Find command (Search menu) opened by pressing Help button in Find dialog. This page contains also a link to help page Regular Expressions (Legacy) explaining the regular expression characters for the legacy regular expression engines UltraEdit and Unix. Another help page explains the most important Perl regular expression characters. The Perl engine is the most powerful engine, but is also the more difficult to learn because of the many options it offers.

          Okay, now let us analyze the search string.

          % matches the start of line. Indicates the search string must be at the beginning of a line but does not include any line terminator characters in the resulting string selected.

          ^(...^) brackets or tags an expression to use in the replace command. A regular expression may have up to 9 tagged expressions, numbered according to their order in the regular expression. In this search string there is only one which is referenced in the replace string by ^1. There is also the power tip tagged expressions explaining the technique of re-using a part of the found string in the replace string.

          * matches any number of occurrences of any character except newline.

          ^p matches a newline (CR/LF) (paragraph) (DOS Files).

          $ matches the end of line. Indicates the search string must be at the end of line but does not include any line terminator characters in the resulting string selected.

          So the search string means: Start search at beginning of a line (%), find any nummber of any characters in this line up to DOS line termination and remember this string for being used in the replace (^(*^)^p), and find any nummber of any characters on next line up to end of line (*$). If you execute once a Find with this UltraEdit regular expression string, you will see that it selects one line completely with the DOS line terminator and the next line up to end of line without the DOS line terminator.

          The replace string simple references the found string selected on first line without the line terminator. As a result the replace deletes the DOS line termination of the first selected line and the string on second line and the DOS line termination on second line becomes the DOS line termination of first line. That's it. So this replace simple selects two lines and deletes the second line.

          For deleting all even lines this regular expression replace all command is executed from top of the file keeping all odd lines and deleting all even lines. For deleting all odd lines the same regular expression replace all command is used to delete every second line, but by executing it now beginning from second line, all odd lines are deleted except the first at top of the file which is finally deleted with an additional command.

          To keep just every 5th line and delete all others you can use

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          Bottom
          IfColNumGt 1
          InsertLine
          IfColNumGt 1
          DeleteToStartofLine
          EndIf
          EndIf
          Top
          Find RegExp "
          %*^p*^p*^p*^p^(*^)$"
          Replace All "^1"


          There is just one problem with that macro respectively the search string. If the file has for example 13 lines, the file contains correct the lines 5 and 10, but wrongly also the lines 11 to 13 after running this macro. It would be necessary to do something to find out which line remains to delete them also with a second replace.

          The following macro has the additional steps required to identify the remaining lines and delete them. The macro first inserts a character at start of every line which never does exist at start of a line. With the replace keeping only every Nth line this special marker character is removed from the lines which should be kept. The remaining lines can now be identified by the marker character and can be easily deleted.

          InsertMode
          ColumnModeOff
          HexOff
          UnixReOff
          Bottom
          IfColNumGt 1
          InsertLine
          IfColNumGt 1
          DeleteToStartofLine
          EndIf
          EndIf
          Top

          Find RegExp "%"
          Replace All "
          »"
          Find RegExp "%*^p*^p*^p*^p»^(*^)$"
          Replace All "^1"

          Find RegExp "%»*^p"
          Replace All ""
          Find RegExp "%
          »"
          Replace All ""


          You can see that I used the character » as marker character. The last before regular expression replace all deletes the remaining lines at bottom of the file, the last regular expression replace all deletes just the marker character » appended at end of the file.

          But it can be annoying to always need to modify the macro to use it for a variable line number. Therefore I converted the macro codes to a script code. The script below asks the user which Nth lines to keep and deletes all other lines using the best method.

          Code: Select all

          if (UltraEdit.document.length > 0) { // Is any file opened?
          
             // Ask user which Nth lines to keep. All others are deleted.
             var nLineNum = UltraEdit.getValue("Enter number of every Nth line to keep:",1);
             if (nLineNum > 0) {
          
                // Define environment for this script.
                UltraEdit.insertMode();
                if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                UltraEdit.activeDocument.hexOff();
                UltraEdit.activeDocument.bottom();
                UltraEdit.ueReOn();
          
                // Define line terminator type by default for DOS. If the version of
                // UE/UES has support for the line terminator property, use it to set
                // the line terminator string according to the value of this property.
                var sLineTerm = "^p";
                var bMacWorkaround = false;
                if (typeof(UltraEdit.activeDocument.lineTerminator) == "number") {
                   if (UltraEdit.activeDocument.lineTerminator == 1) {
                      sLineTerm = "^n";  // UNIX file not converted to DOS.
                   }
                   else if (UltraEdit.activeDocument.lineTerminator == 2) {
                      // sLineTerm = "^r";
                      // Just using ^r should work, but depending on version of UE
                      // or UES the result could be that just the first Nth - 1 lines
                      // block at top of the file is deleted or just the first line.
                      // A workaround is to convert the file temporarily to DOS.
                      UltraEdit.activeDocument.unixMacToDos();
                      bMacWorkaround = true;
                   }
                }
          
                // Make sure that file ends with a line termination.
                if (UltraEdit.activeDocument.isColNumGt(1)) {
                   UltraEdit.activeDocument.insertLine();
                   if (UltraEdit.activeDocument.isColNumGt(1)) {
                      UltraEdit.activeDocument.deleteToStartOfLine();
                   }
                }
          
                // Define the parameters for all regular expression replaces
                // done with the UltraEdit regular expression engine.
                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.preserveCase=false;
                UltraEdit.activeDocument.findReplace.replaceAll=true;
                UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
                // To avoid a script execution error on UltraEdit
                // versions not supporting searching in columns.
                if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
                   UltraEdit.activeDocument.findReplace.searchInColumn=false;
                }
          
                // To delete all odd lines, use a simplified method deleting every
                // second line beginning from line 2 and then delete the first line.
                if (nLineNum == 2) {
                   UltraEdit.activeDocument.gotoLine(2,1);
                   UltraEdit.activeDocument.findReplace.replace("%^(*^)"+sLineTerm+"*$", "^1");
                   UltraEdit.activeDocument.top();
                   UltraEdit.activeDocument.deleteLine();
                }
                // Deleting all even lines can be made very simple.
                else if (nLineNum == 1) {
                   UltraEdit.activeDocument.top();
                   UltraEdit.activeDocument.findReplace.replace("%^(*^)"+sLineTerm+"*$", "^1");
                }
                // Keep just every Nth line is more complicated because remaining
                // lines at bottom of the file must be identified and deleted too.
                else {
                   // Build the regular expression search string to
                   // delete all lines except the line to keep.
                   var sRegExp = '*' + sLineTerm;
                   var sSearchExp = "%";
                   while (--nLineNum) sSearchExp += sRegExp;
                   sSearchExp += "»^(*^)$"
                   var_dump(sSearchExp);
                   UltraEdit.activeDocument.top();
                   // Insert the marker character » at beginning of every line.
                   UltraEdit.activeDocument.findReplace.replace("%", "»");
                   // Delete all lines except every Nth lines.
                   UltraEdit.activeDocument.findReplace.replace(sSearchExp, "^1");
                   // Delete all lines remaining at bottom of the file.
                   UltraEdit.activeDocument.findReplace.replace("%»*"+sLineTerm, "");
                   // Delete the marker character » appended to end of file.
                   UltraEdit.activeDocument.findReplace.replace("%»", "");
                }
                if (bMacWorkaround) UltraEdit.activeDocument.dosToMac();
             }
          }
          Edit on 2011-08-31:
          Script works now also for UNIX and MAC files not converted temporarily to DOS on opening the file if version of UltraEdit or UEStudio offers the lineTerminator property for active file.

          2
          NewbieNewbie
          2

            Aug 31, 2011#5

            This a great script. But maybe you can help to modify it. What if instead I want to delete the Nth-lines and keep the rest? Thanks a lot.

            6,675585
            Grand MasterGrand Master
            6,675585

              Aug 31, 2011#6

              No problem. Here is the script to delete every Nth line and keep all others.

              It works also for UNIX and MAC files not converted temporarily to DOS on opening the file if version of UltraEdit or UEStudio offers the lineTerminator property for active file.

              Code: Select all

              if (UltraEdit.document.length > 0) { // Is any file opened?
              
                 // Ask user which Nth line to delete. All others are kept.
                 var nLineNum = UltraEdit.getValue("Enter number of every Nth line to delete:",1);
                 if (nLineNum > 0) {
              
                    // Define environment for this script.
                    UltraEdit.insertMode();
                    if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                    else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
                    UltraEdit.activeDocument.hexOff();
                    UltraEdit.activeDocument.bottom();
                    UltraEdit.ueReOn();
              
                    // Define line terminator type by default for DOS. If the version of
                    // UE/UES has support for the line terminator property, use it to set
                    // the line terminator string according to the value of this property.
                    var sLineTerm = "^p";
                    var bMacWorkaround = false;
                    if (typeof(UltraEdit.activeDocument.lineTerminator) == "number") {
                       if (UltraEdit.activeDocument.lineTerminator == 1) {
                          sLineTerm = "^n";  // UNIX file not converted to DOS.
                       }
                       else if (UltraEdit.activeDocument.lineTerminator == 2) {
                          // sLineTerm = "^r";
                          // Just using ^r should work, but depending on version of UE
                          // respectively UES the result could be that just the first
                          // Nth - 1 lines block at top of the file is deleted. A
                          // workaround is to convert the file temporarily to DOS.
                          UltraEdit.activeDocument.unixMacToDos();
                          bMacWorkaround = true;
                       }
                    }
              
              
                    // Make sure that file ends with a line termination.
                    if (UltraEdit.activeDocument.isColNumGt(1)) {
                       UltraEdit.activeDocument.insertLine();
                       if (UltraEdit.activeDocument.isColNumGt(1)) {
                          UltraEdit.activeDocument.deleteToStartOfLine();
                       }
                    }
              
                    // Define the parameters for all regular expression
                    // replaces done with the Perl regular expression engine.
                    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.preserveCase=false;
                    UltraEdit.activeDocument.findReplace.replaceAll=true;
                    UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
                    // To avoid a script execution error on UltraEdit
                    // versions not supporting searching in columns.
                    if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
                       UltraEdit.activeDocument.findReplace.searchInColumn=false;
                    }
              
                    // To delete all odd lines, use a different method deleting every
                    // second line beginning from line 2 and then delete the first line.
                    // This method is necessary for files with an odd number of lines.
                    if (nLineNum == 1) {
                       UltraEdit.activeDocument.gotoLine(2,1);
                       UltraEdit.activeDocument.findReplace.replace("%^(*^)"+sLineTerm+"*$", "^1");
                       UltraEdit.activeDocument.top();
                       UltraEdit.activeDocument.deleteLine();
                    }
                    // Deleting every other Nth line is done with a dynamically created
                    // UltraEdit regular expression engine search string using a single
                    // Replace All command.
                    else {
                       // Build the search string to delete every Nth line.
                       var sRegExp = '*' + sLineTerm;
                       var sSearchExp = "%^(";
                       while (--nLineNum) sSearchExp += sRegExp;
                       sSearchExp += "^)" + sRegExp;
                       UltraEdit.activeDocument.top();
                       UltraEdit.activeDocument.findReplace.replace(sSearchExp, "^1");
                    }
                    if (bMacWorkaround) UltraEdit.activeDocument.dosToMac();
                 }
              }

              2
              NewbieNewbie
              2

                Sep 01, 2011#7

                Thanks, pal for the quick reply. will test the script in while. thanks again