Community
    • Login

    How to delete specific lines only with macro

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    5 Posts 2 Posters 205 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dominic LeblancD
      Dominic Leblanc
      last edited by

      Hi,

      I have a text file that lists georeferenced line in UTM format and should go like this: line number, coordinate of the first point and coordinate of the last point.

      Example:
      L106900
      359088.16 4927710.64
      372696.93 4924424.02

      But, when the file was made, it had generated a bunch of coordinates in between the first and last coordinates that I would like to get rid of. I started doing it manually but it’s thousands of lines I need to remove.

      Any idea how to automate that?

      Thanks,
      D

      1 Reply Last reply Reply Quote 0
      • guy038G
        guy038
        last edited by guy038

        Hello, @dominic-leblanc and All,

        Well, here is a possible solution , using a regular expression


        Given this example of INPUT text :

        L106900
        359088.16 4927710.64
        368425.29 4925689.13
        359928.56 4926782.72
        372696.93 4924424.02
        
        
        L106901
        359261.78 4932588.32
        375624.69 4926854.61
        359923.84 4926958.17
        378952.70 4937532.29
        396284.33 4925818.24
        382621.65 4920105.59
        
        

        And the following regex S/R :

        SEARCH/MARK (?-is)^L\d+\R.+\R\K(?s:.*?)(?=.+(?:\R+L\d+|\R*\z))

        REPLACE Leave EMPTY

        You would be left, after execution, with this OUTPUT text :

        L106900
        359088.16 4927710.64
        372696.93 4924424.02
        
        
        L106901
        359261.78 4932588.32
        382621.65 4920105.59
        
        

        As you can see, this regex :

        • Keep the first line after a line beginning the uppercase letter L

        • Keep the last line before a line beginning the uppercase letter L or before the end of text / file

        • Delete any coordinates line located in between


        IMPORTANT :

        • You must select the Regular expression search mode

        • You may check the Wrap around option, if you need to modify the whole text of current file

        • You can match the different occurrences, using the Find dialog and the Find Next button

        • You can see the bunch of all the lines to delete , using the Mark dialog and the Mark All button

        • When using the Replace dialog, you must use, exclusively the Replace All button ( NOT the Replace button ) because of the \K syntax part of the regex search expression !

        • Each line beginning with an L letter and digits may be preceded or not with some empty lines

        • The last bunch or coordinates lines may be followed or not with some empty lines

        Best Regards,

        guy038

        P.S. :

        If, by mistake, you re-run this regex search/replacement, you may be surprised of the message Replace All: n occurrences were replaced .... That is quite normal, because it replaces, this time, the zero-length bunch of in-between lines with nothing again !

        Dominic LeblancD 1 Reply Last reply Reply Quote 2
        • Dominic LeblancD
          Dominic Leblanc @guy038
          last edited by

          @guy038

          Wow! I’m impressed thank you! It didn’t work for some lines for which the number has a decimal though like L102300.1

          1 Reply Last reply Reply Quote 1
          • guy038G
            guy038
            last edited by guy038

            Hi, @dominic-leblanc and All,

            Ah…, OK ! As this fact wasn’t mentionned in your initial example, this case was not supposed to happen :-)

            So, my new version of the search regex is simply :

            • SEARCH/MARK (?-is)^L[\d.]+\R.+\R\K(?s:.*?)(?=.+(?:\R+L[\d.]+|\R*\z))

            Note that this version would also match if the initial georeferenced line was, for instance :

            L.12.34.56.78.9
            

            I suppose that it’s not a problem if your text does not contain such lines ;-))

            BR

            guy038

            1 Reply Last reply Reply Quote 2
            • Dominic LeblancD
              Dominic Leblanc
              last edited by

              Thanks Guy, I really appreciate your help!

              1 Reply Last reply Reply Quote 1
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors