Select a section with search, count and remove lines in a section

Select a section with search, count and remove lines in a section

1581
Power UserPower User
1581

    May 27, 2020#1

    I have a lot of similar report lines in "groups", looking like this:
    • two headers
    • a separation line
    • a header, beginning with FID
    • a group of lines (number + tab + name), ending with empty lines. Usually some hundred lines long.
    • the next "group" begins

    Code: Select all

    Prüfungsname: Kontakt ohne Art
    Beschreibung: 
    ---------------------------------------------------------------------------------------------
    FID: Feature Class:
    10 Kontakt
    9 Kontakt
    12 Kontakt
    11 Kontakt
    
    
    What I need: Count the length of the "group" (here: 4) and delete the group.

    What I do manually:
    • I select the position at the beginning of string "FID",
    • select the entire section until the end, define with two paragraphs (^p^p),
    • count all lines in the selection,
    • remove the selection.
    I tried it with "record a macro", but failed.
    Is there a way to do it (semi)-automatically?

    Regards, Peter
    UE 26.20.0.74 German / Win 10 x 64 Pro

    6,686585
    Grand MasterGrand Master
    6,686585

      May 27, 2020#2

      This is of course possible with a script. Here is a demonstration code.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
      
         // Make sure the last line in file has also a line termination.
         UltraEdit.activeDocument.bottom();
         if (UltraEdit.activeDocument.isColNumGt(1))
         {
            UltraEdit.activeDocument.insertLine();
            if (UltraEdit.activeDocument.isColNumGt(1))
            {
               UltraEdit.activeDocument.deleteToStartOfLine();
            }
         }
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define the parameter for a case-insensitive Perl regular expression find.
         UltraEdit.perlReOn();
         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;
      
         // Search in a loop for the blocks and for each block determine the
         // number of line terminations and subtract the four line terminations
         // of the heder to get the variable number of data lines. Output this
         // information to the output window and continue with next search.
         var nBlockNumber = 0;
         while (UltraEdit.activeDocument.findReplace.find("Prüfungsname:.*\\r?\\nBeschreibung:.*\\r?\\n-{70,}\\r?\\nFID: Feature Class:.*\\r?\\n(?:\\d+ .*\\r?\\n)*"))
         {
            nBlockNumber++;
            var asLineTerms = UltraEdit.activeDocument.selection.match(/\r?\n/g);
            var nLineCount = asLineTerms.length - 4;
            UltraEdit.outputWindow.write("Block " + nBlockNumber + " has " + nLineCount + " line" + ((nLineCount==1) ? "." : "s."));
      //      UltraEdit.activeDocument.deleteText();
         }
         if (nBlockNumber)
         {
            UltraEdit.outputWindow.showWindow(true);
            UltraEdit.activeDocument.top();
         }
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      1581
      Power UserPower User
      1581

        May 28, 2020#3

        Thanks Mofi, for your great support.

        There are 2 problems:
        1. The file contains more strings then showed above. So the script has problems with these lines and report errors. My fault - I didn't mention them. The script runs without error when I reduce the additional lines.
        2. It reports in output window "Script succeeded" - but nothing else happens.
        Here is a more realistic file snippet again; but the "block" can contain up to 4000 lines.

        Code: Select all

        **************************************************************
        Map 3D-Datenprüfung
        Datum: 28.05.2020 Uhrzeit: 09:06
        **************************************************************
        HOAW
        
        Prüfungsname: Einzelfläche ohne Bezeichnung
        Beschreibung: 
        ---------------------------------------------------------------------------------------------
        ---
        
        Prüfungsname: Einstiegshilfe - fehlende Obligatorien 
        Beschreibung: Lösung: prüfen, nachtragen
        ---------------------------------------------------------------------------------------------
        ---
        
        
        Prüfungsname: Haltung ohne Bezeichnung
        Beschreibung: 
        ---------------------------------------------------------------------------------------------
        FID:    Feature Class:
        5512    Haltung
        1252    Haltung
        3276    Haltung
        5996    Haltung
        6146    Haltung
        5180    Haltung
        6073    Haltung
        5659    Haltung
        4070    Haltung
        5011    Haltung
        8196    Haltung
        7670    Haltung
        5592    Haltung
        6915    Haltung
        7590    Haltung
        8835    Haltung
        8966    Haltung
        
        Prüfungsname: Vorflutereinlauf ohne Bezeichnung
        Beschreibung: 
        ---------------------------------------------------------------------------------------------
        ---
        
        Prüfungsname: Haltung - unklare Profilbreiten  
        Beschreibung: Profilbreite 
        ---------------------------------------------------------------------------------------------
        FID:    Feature Class:
        52072    Haltung
        51623    Haltung
        52495    Haltung
        52040    Haltung
        52090    Haltung
        52030    Haltung
        51605    Haltung
        463    Haltung
        52498    Haltung
        
        Prüfungsname: PAA Leitungen 
        Beschreibung: 
        ---------------------------------------------------------------------------------------------
        ---
        
        UE 26.20.0.74 German / Win 10 x 64 Pro

        6,686585
        Grand MasterGrand Master
        6,686585

          May 28, 2020#4

          Try the script with this line:

          Code: Select all

            while (UltraEdit.activeDocument.findReplace.find("Prüfungsname:.*\\r?\\nBeschreibung:.*\\r?\\n[\\r\\n\\-]++FID:[\\t ]+Feature Class:.*\\r?\\n(?:\\d+[\\t ].*\\r?\\n)*"))
          Best regards from an UC/UE/UES for Windows user from Austria

          1581
          Power UserPower User
          1581

            May 28, 2020#5

            Code: Select all

            \\r?\\n[\\r\\n\\-]++FID:[\\t ]+Feature Class:.*\\r?\\n(?:\\d+[\\t ].*\\r?\\n)*"))
            I just want to know - is this ..
            a) Captain Kirk, Space Ship Enterprise, reporting to the log-book or
            b) an old couple, talking together (see: Loriot, Das 3-Minuten-Ei)

            Thanks!!
            UE 26.20.0.74 German / Win 10 x 64 Pro