Delete all lines not starting with a special phrase

Delete all lines not starting with a special phrase

19
Basic UserBasic User
19

    Jun 20, 2006#1

    Hi,
    Since I'm a newbie I'm not sure how to write a Macro for my problem:
    Just keep the lines starting with a special phrase. (If the phrase is in the middle of a line: delete line too)

    Before:
    This is Line 1
    And another Line 2
    Again another Line3
    Starting phrase blablabla1
    Starting phrase blabla2
    Another Line 4

    After:
    Starting phrase blablabla1
    Starting phrase blabla2

    6,687587
    Grand MasterGrand Master
    6,687587

      Jun 20, 2006#2

      Not need to do this with a macro. Use the Find dialog with following UltraEdit style regex search string %Starting phrase and enable also List Lines Containing String.

      In the dialog shown after clicking on the Find button, click on Clipboard to copy all the lines found with the starting phrase to the clipboard and Close the dialog. Next select all with Ctrl+A and paste the lines found.

      But if you want to do it with a macro because you need this regularly, use the following macro with macro property Continue if a Find with Replace not found enabled.

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Clipboard 9
      ClearClipboard
      Top
      Loop
      Find MatchCase RegExp "%Starting phrase*^p"
      IfFound
      CopyAppend
      Else
      ExitLoop
      EndIf
      EndLoop
      SelectAll
      Paste
      Top
      ClearClipboard
      Clipboard 0

      Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.

      Note: If your starting phrase contains UltraEdit style regular expression characters you must escape it with the ^ character.


      Here is a more complex one which asks you for the starting phrase and automatically escapes UE style regex characters.

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOff
      Bottom
      IfColNum 1
      Else
      "
      "
      EndIf
      Top
      Clipboard 8
      GetString "Enter the starting phrase:"
      "
      "
      SelectToTop
      Find RegExp "^([^$^%^*^+^.^?^[^]^^]^)"
      Replace All SelectText "^^^^^^1"
      EndSelect
      Top
      StartSelect
      Key END
      Cut
      EndSelect
      Key DEL
      Clipboard 9
      ClearClipboard
      Loop
      Clipboard 8
      Find MatchCase RegExp "%^c*^p"
      IfFound
      Clipboard 9
      CopyAppend
      Else
      ExitLoop
      EndIf
      EndLoop
      ClearClipboard
      Clipboard 9
      SelectAll
      Paste
      ClearClipboard
      Clipboard 0
      Best regards from an UC/UE/UES for Windows user from Austria

      19
      Basic UserBasic User
      19

        Jun 21, 2006#3

        Mofi, your macro works, but 3 questions:
        How does you Macro know if the EOF is reached for exiting the loop (the Macro works without IfEof)?

        Is there a macro which first selects all consecutive lines and copy them in one sweep to the clipboard (like I would do it manually, it's quicker than copying every single line)?

        In general, is it possible to search for lines NOT containing a search-string?
        (e.g: Find RegExp NOT(%Starting phrase*^p)

        6,687587
        Grand MasterGrand Master
        6,687587

          Jun 21, 2006#4

          thomasm76 wrote:How does you Makro know if the EOF is reached for exiting the loop (the Makro works without IfEof)?
          The loop in the macro is executed until no line with the starting phrase is found anymore. See

          IfFound
          ...
          Else
          ExitLoop
          EndIf

          Many of my macros do not run till EOF, because they searches from top down until the searched string is not found anymore. Within a macro execution the configuration option Continue find at End of File is ignored. (Except if Perl engine is enabled where still user dialogs are displayed if a string is not found till EOF while executing a search in a macro loop. This is of course a bug of v12.00 till current version 12.10a.)
          thomasm76 wrote:Is there a Makro which first selects all consecutive lines and copy them in one sweep to the clipboard (like I would do it manually, it's quicker than copying every single line)?
          Such a block selecting code is used several times in the posted macros. For example you have found the starting phrase in a line and want to copy everything from start of this line till next blank line (without the blank line).

          UnixReOff
          Key HOME
          IfColNumGt 1
          Key HOME
          EndIf
          StartSelect
          Find RegExp Select "^p[ ^t]++^p"
          Key UP ARROW
          Copy
          EndSelect

          If your DOS terminated file does not contain trailing spaces on blank lines (because you have used TrimTrailingSpaces at start of the macro), this can be done without regexp search:

          Key HOME
          IfColNumGt 1
          Key HOME
          EndIf
          StartSelect
          Find Select "^p^p"
          Key UP ARROW
          Copy
          EndSelect

          And if you have enabled the configuration setting Home Key Always Goto Column 1 a single Key HOME is also enough:

          Key HOME
          StartSelect
          Find RegExp Select "^p^p"
          Key UP ARROW
          Copy
          EndSelect

          You see, I have to think about many problems when writing macros for general purposes posted here because I don't know the content of the file and the UE settings of the user.
          thomasm76 wrote:In general, is it possible to search for lines NOT containing a search-string?
          (e.g: Find RegExp NOT(%Starting phrase*^p)
          As already written several times - no! Maybe with the Perl engine. I don't know too much about Perl regex. See for example How to delete lines which do not contain specific word.
          Best regards from an UC/UE/UES for Windows user from Austria

          2
          NewbieNewbie
          2

            Jun 16, 2010#5

            Hi,

            I am new in this and I need to know how I can delete all line in a file except those starting with 12a and 455

            Ex:

            12aererwerwrwer
            23sdrgferf4545r
            455ererwerwrwer
            12athgret54553
            563ee44232dddd

            And I need to keep all the lines starting with : 12a and 455

            Thank you.

            John

            6,687587
            Grand MasterGrand Master
            6,687587

              Jun 17, 2010#6

              This can be done with executing a Perl regular expression replace all command.

              Find What: ^(?!12a|455).*\r\n
              Replace With: empty

              As macro code:

              PerlReOn
              Find RegExp "^(?!12a|455).*\r\n"
              Replace All ""
              Best regards from an UC/UE/UES for Windows user from Austria

              2
              NewbieNewbie
              2

                Jun 17, 2010#7

                Absolutly amazing.

                So easy with people who know how it's work.

                Thank you.