Community
    • Login

    delete line between bookmark

    Scheduled Pinned Locked Moved General Discussion
    45 Posts 4 Posters 14.1k 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.
    • guy038G
      guy038
      last edited by

      Hello, @pinuzzu99, @alan-kilborn All,

      Based on the regexes given in the post, below :

      https://community.notepad-plus-plus.org/post/50272

      I think that the similar regex S/R, below, should do the job !

      SEARCH (?-s)^(?!.+@).*\R?

      REPLACE Leave EMPTY

      Notes :

      • The first part (?-s) is an in-line modifier, which means that any regex dot symbol ( . ) matches a single standard character

      • Then the (?!.+@) is a negative look-ahead structure , which means that , from beginning of line, any non-null range of chars till a @ symbol must not exist.

      • If it’s the case ( So, a line without an e-mail address ! ) the final part .*\R? matches any range, even null, of chars, from beginning of line, followed with its possible line-break

      • As the replacement field is empty, any line, not containing a @ symbol, is then deleted !


      However, @pinuzzu99, it would be good if you follow Alan’s advice and try to learn, at least, the basics of regular expressions!

      No doubt, you will make big mistakes at first, but little by little, you will understand their syntax and find a particular regular expression for each specific problem. Believe me, after a certain point, you won’t go back ;-))

      Best Regards,

      guy038

      1 Reply Last reply Reply Quote 2
      • Alan KilbornA
        Alan Kilborn @pinuzzu99
        last edited by Alan Kilborn

        @pinuzzu99 said in delete line between bookmark:

        I’d like to have a function that eliminates lines within a certain range.

        If you’re willing to use the Pythonscript plugin, here is a script that will do such a thing:

        # -*- coding: utf-8 -*-
        
        from Npp import editor, notepad
        
        def main():
        
            user_input = ''
            while True:
                user_input = notepad.prompt('Enter first line # of range to delete:', '', user_input)
                if user_input == None: return  # user pressed Cancel
                try:
                    if user_input.lower() == 'bof': user_input = 1
                    elif user_input.lower() == 'eof': user_input = editor.getLineCount()
                    line1 = int(user_input)
                    if not (1 <= line1 <= editor.getLineCount()): raise ValueError
                except ValueError:
                    continue
                break
            user_input = ''
            while True:
                user_input = notepad.prompt('Enter last line # of range to delete:', '', user_input)
                if user_input == None: return  # user pressed Cancel
                try:
                    if user_input.lower() == 'bof': user_input = 1
                    elif user_input.lower() == 'eof': user_input = editor.getLineCount()
                    line2 = int(user_input)
                    if not (1 <= line2 <= editor.getLineCount()): raise ValueError
                except ValueError:
                    continue
                break
            line1 -= 1; line2 -= 1  # convert "user" line numbers to "system" line numbers
            if line1 > line2: (line1, line2) = (line2, line1)
            start_pos = editor.positionFromLine(line1)
            end_pos = editor.positionFromLine(line2) + len(editor.getLine(line2))
            editor.beginUndoAction()
            editor.deleteRange(start_pos, end_pos - start_pos)
            editor.endUndoAction()
        
        main()
        
        

        In addition to using integer line numbers at the prompts, you can also use BOF for beginning-of-file and/or EOF for end-of-file.

        1 Reply Last reply Reply Quote 1
        • pinuzzu99P
          pinuzzu99
          last edited by

          @guy038 tanxs for your reply, always highly appreciated!
          but your string with replace, delete only one line at time; with replace all delete all line.
          this is another specific case, similar but different from the post you indicated above…
          here I want to delete only a range of lines, between 2 emails, but only that, not all lines on my txt. I need to see what they contain first, then delete that range, but only that, not all on only one shot.
          I need a function like Begin/End Select, but with this function to put an End to the selection I have to scroll all the lines to the bottom, and sometimes I have intervals of even 10,000 lines…
          so I need a command that, given the first few lines, if I consider it ok, I want to delete them all but only until the next email. I need a command to delete lines INCLUDED in 2 emails. only those, not all.

          @ Alan
          tanxs also for your suggestion. now i try it.

          1 Reply Last reply Reply Quote 1
          • pinuzzu99P
            pinuzzu99
            last edited by pinuzzu99

            Alan, please, explain me how to work.
            installed python plugin, copy your script and saved into plug folder on python, i don’t understand ho to insert range into script.
            see my shot above, i need delete line between 10-28

            alt text

            Alan KilbornA 1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @pinuzzu99
              last edited by Alan Kilborn

              @pinuzzu99

              Hmmmmmm, well, first, you don’t need the boomarks. I’m not sure if you put them in just for running the script, or if they were just already there.

              Second, you have to read the prompt on the box: Enter first line # of range to delete: where, for your example, you would enter 10:

              a5154e5b-d45c-4bde-9abf-f17d904af261-image.png

              When you say OK to that, it will ask you for the last line, where you would enter 28:

              117b228e-ca0a-4d4d-85af-b79156847b13-image.png

              When you do that and press OK, it will then delete your range of lines, including the first and last (10 and 28 in this example will be gone after the final OK, as well as all lines in between).

              You can press Cancel in either box to abort everything.

              Did I really make it that hard to understand??

              Sure, it could all be combined into one box, but then one has to think more about what to put in a single input box. The way I did it, you just follow the prompts.

              By the way, your first line could have been 28 and your last line 10 and it would have worked the same way.

              1 Reply Last reply Reply Quote 1
              • Alan KilbornA
                Alan Kilborn
                last edited by

                Hindsight looking at the script:

                Where I call the lower function on the user_input variable probably isn’t the best place for it. They won’t fail with a ValueError and thus probably shouldn’t be part of that block. But the script DOES work as written (AFAICT), so I won’t provide a changed version.

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

                  @pinuzzu99, @alan-kilborn All,

                  Sorry, I did not understand your need, correctly ! Indeed, you simply want, from a line, containing an e-mail address, delete all subsequent lines till the next line containing an e-mail address, excluded, don’t you ?

                  If so :

                  • Move to the line right after a line containing an e-mail address

                    • Manually

                    • Using the regex (?-s).+@.+\R\K, which places the caret at beginning of any line right after an e-mail address

                  • Now, use the following regex S/R to delete all lines till the next e-mail address line

                    • SEARCH (?s).+?(?=(?-s)^.+@)

                    • REPLACE Leave EMPTY

                  Note : I gave it a try, with 30,000 random lines between two lines containing an e-mail address and it correctly selected all this stuff in between and deleted it ;-))

                  Best Regards,

                  guy038

                  1 Reply Last reply Reply Quote 0
                  • pinuzzu99P
                    pinuzzu99
                    last edited by pinuzzu99

                    tanxs Alan, this was what i wanted!
                    and the bookmarks were only to make it clear the range i wanted to eliminate…

                    @guy038
                    tanxs for your reply. but i was hoping it could be done in one step…
                    anyway work well, always thanks for your appreciated help!

                    Alan KilbornA 1 Reply Last reply Reply Quote 1
                    • Alan KilbornA
                      Alan Kilborn @pinuzzu99
                      last edited by Alan Kilborn

                      @pinuzzu99 said in delete line between bookmark:

                      but i was hoping it could be done in one step

                      This implies that it is something you do often, so it might be worth recording multiple steps into a “macro” that you keep. This thread talks more about that.

                      Script notes: Using bof doesn’t make much sense – it is always going to be line 1 so why not just use 1? Also, I’ve thought of some extensions to the script. I could see myself using the script often. I’ll posts an enhanced version soon!

                      1 Reply Last reply Reply Quote 1
                      • pinuzzu99P
                        pinuzzu99
                        last edited by

                        tanxs for your explanation on macros.
                        but, if i understand correctly, if i record a macro, for example by deleting lines 10 to 20, this macro always repeats the same procedure…
                        I instead have different txt, in which the lines to be deleted are always different…
                        so i think in my specific case the elimination steps have to be done manually.
                        is this correct?

                        Alan KilbornA 1 Reply Last reply Reply Quote 0
                        • pinuzzu99P
                          pinuzzu99
                          last edited by

                          @guy038
                          also your string work, but this delete all line, including line which contains the email…
                          instead i want delete the lines under the email, but leave this …

                          1 Reply Last reply Reply Quote 0
                          • Alan KilbornA
                            Alan Kilborn @pinuzzu99
                            last edited by

                            @pinuzzu99

                            I thought we were talking about several replacement operations when I brought up recording in macros. Other operations don’t lend themselves so well to “macroization”.

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

                              Hello, @pinuzzu99 and All,

                              You said in a previous post :

                              also your string work, but this delete all line, including line which contains the email…
                              instead i want delete the lines under the email, but leave this …

                              My method does not delete any -mail address, assuming that you put the caret at the beginning of the first line, right after the line containing this e-mail address !

                              Consider the input text :

                              ....
                              ....
                              ....
                              jasortrtn25hoon@hotmail.com:bartrall2335
                              2017-07-08 12.35.03_5.jpg                         			32KB			             08/07/2017 19:34:57
                              2017-07-08 12.35.03_8.jpg                         			71KB			             08/07/2017 19:34:57
                              2017-07-08 12.35.10_4.jpg                         			70KB			             08/07/2017 19:35:04
                              2017-07-08 12.48.43_15.jpg                        			84KB			             08/07/2017 19:48:41
                              2017-07-25 01.22.59.jpg                           			102KB			            25/07/2017 08:22:59
                              ======================================
                              skysfsfff@gmail.cam:ksddsfdsfy37
                              ....
                              ....
                              ....
                              
                              • If you run the search regex (?-s).+@.+\R\K, the caret should have moved at the beginning of the line 2017-07-08 12.35.03_5.jpg, with the calltip ^zero length match

                              • Then, after running the search regex (?s).+?(?=(?-s)^.+@) all the lines, after the jasortrtn25hoon@hotmail.com, till the second e-mail address skysfsfff@gmail.cam excluded, should have been selected, then deleted after replacement !


                              But, may be, you’ll prefer this other method, which need one operation, only !

                              • Move , first, to a line containing an e-mail address, if necessary. Note that the caret can be at any position in that line, with an e-mail address !

                              • Then, use the following regex S/R to delete all lines, after this 1st e-mail address till the next e-mail address, excluded

                              SEARCH (?-s).*\R\K(?s:.+?)(?=^.+@)

                              REPLACE Leave EMPTY

                              Cheers,

                              guy038

                              1 Reply Last reply Reply Quote 0
                              • pinuzzu99P
                                pinuzzu99
                                last edited by

                                with first string work fine, but at one specific account all text it is selected, then deleted! but only in a specific case, maybe it contains characters that other accounts don’t contain …
                                try with this txt: https://www.upload.ee/files/11106151/try_this.txt.html

                                so, second string work, select all line that I want to eliminate, but then with the replace the lines are not deleted!
                                i have record my screen, see video here: https://www.upload.ee/files/11106092/video_02.mp4.html
                                maybe i do something wrong…

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

                                  @pinuzzu99 and All,

                                  Oh… I forgot to mention that you need to use the Replace All button , only. Do not use the Replace button which does not act as it should, when the regex contain any \K syntax. This is a general rule, available for any regex containing, at least, one \K syntax

                                  Best Regards,

                                  guy038

                                  1 Reply Last reply Reply Quote 2
                                  • pinuzzu99P
                                    pinuzzu99
                                    last edited by pinuzzu99

                                    yes i know, Replace All and i have try it before my post above, but as i said, i want check all my single block one by one before delete all lines.
                                    for my purpose work fine this: (?-s).*\R(?s:.+?)(?=^.+@)
                                    without \K and work with Replace, but on next block i need to put cursor at begin line under mail line…
                                    this is little problem if my txt have 10,000 block of mail - file list…

                                    1 Reply Last reply Reply Quote 0
                                    • pinuzzu99P
                                      pinuzzu99
                                      last edited by pinuzzu99

                                      in this specific case string (?-s).*\R(?s:.+?)(?=^.+@) don’t find value…
                                      why? what is different than other blocks?

                                      earlnathanieluhftr@gmail.com:natchel7634
                                      Used: 3/50GB
                                      Physiology                                        			0KB
                                      Internal Med                                      			0KB
                                      Passwords.zip                                     			370KB
                                      Lippincott Illustrated Reviews Flash Cards Physiology - Preston, Robin [SRG].pdf			42137KB			          08/09/2015 17:46:42
                                      Ganongs Review of Medical Physiology,  24E (2012) [UnitedVRG].pdf			80607KB			          08/09/2015 17:46:36
                                      Kaplan USMLE Step 1 Physiology Lecture Notes (2013) [PDF] [UnitedVRG].pdf			25312KB			          08/09/2015 12:50:40
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • PeterJonesP
                                        PeterJones
                                        last edited by

                                        @pinuzzu99 said in delete line between bookmark:

                                        i want check all my single block one by one before delete all lines.

                                        But as @guy038 said, that expression won’t work in one-at-a-time mode. Continuing to try is an exercise in futility.

                                        If you are uncertain whether the regex will meet all your needs, and you want to be able to compare the “before” and “after” states of your file, I recommend opening the original file blah.txt, File > Save copy as, give it a name like blah_new.txt, then open blah_new.txt and move to other view – so that the two panes of Notepad++ are blah.txt on the left and blah_new.txt on the right, then run Guy’s regex on the blah_new.txt, then use the Compare plugin (which you can install from the Plugins Admin) to find and easily navigate through the differences between the files, and make sure everything is the way you want it. If they are all right, great; if most are right and just a couple aren’t quite right, then manually fix those last few; if there are many wrong, then run Undo, and try to update the regex to better do what you want. Once you are happy with blah_new.txt, you can delete the original blah.txt (or rename to blah_old.txt, then rename blah_new.txt to blah.txt.

                                        (Or, you can start by copying blah.txt to blah_old.txt, and have blah_old.txt on the left and blah.txt on the right, doing all your edits to blah.txt, and eliminating the extra renames at the end of the procedure.)

                                        1 Reply Last reply Reply Quote 1
                                        • pinuzzu99P
                                          pinuzzu99
                                          last edited by

                                          i already save one backup of my txt…
                                          and yes, compare plugin, i have and already use this.
                                          however Document Map is very useful for scrolling through the file…

                                          1 Reply Last reply Reply Quote 0
                                          • pinuzzu99P
                                            pinuzzu99
                                            last edited by

                                            i hope guy038 reply to my post (4 post above above this post)…

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