• Login
Community
  • Login

Delete every line below a bookmarked line.

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
47 Posts 6 Posters 2.3k 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.
  • A
    Antheds Kennel
    last edited by PeterJones Dec 29, 2024, 3:29 PM Dec 29, 2024, 2:52 PM

    I have a number of documents where I have used different search terms to find lines that I have bookmarked. What I now want is to delete the line below that contains text that is redundant. I have tried making a macro that looks like this: start recording F2->down arrow-> CTRL+i->Delete stop recording. Then I have run the macro on repeat where it goes completely wrong. Is there any kind soul who can guide me in the right direction? Here is an example of how I think.
    Before:

    #STNEW:_TEST-1
    blablablabla
    #STNEW:_TEST-2
    blablablabla
    #STNEW:_TEST-3
    blablablabla
    #STNEW:_TEST-4
    blablablabla
    

    After:

    #STNEW:_TEST-1
    #STNEW:_TEST-2
    #STNEW:_TEST-3
    #STNEW:_TEST-4
    

    Kind regards Thore from Sweden

    —

    moderator added code markdown around text; please don’t forget to use the </> button to mark example text as “code” so that characters don’t get changed by the forum

    D D 2 Replies Last reply Dec 29, 2024, 5:46 PM Reply Quote 1
    • D
      datatraveller1 @Antheds Kennel
      last edited by datatraveller1 Dec 29, 2024, 5:49 PM Dec 29, 2024, 5:46 PM

      @Antheds-Kennel Probably this is not what you mean, but if you have every line bookmarked that you want to keep there is a menu point Search - Bookmark - Remove Non-Bookmarked Lines

      A 1 Reply Last reply Dec 30, 2024, 10:01 AM Reply Quote 3
      • D
        dr ramaanand @Antheds Kennel
        last edited by dr ramaanand Dec 29, 2024, 7:11 PM Dec 29, 2024, 6:19 PM

        @Antheds-Kennel A RegEx will be more useful. Type (#STNEW:_TEST-\d+)(*SKIP)(*F)|\w+ in the Find field, tick/select the Regular expression mode, leave the Replace field blank and hit Replace all - alll the words will disappear. If there are spaces anywhere, please let us know.

        1 Reply Last reply Reply Quote 0
        • G
          guy038
          last edited by guy038 Dec 30, 2024, 10:23 AM Dec 29, 2024, 7:52 PM

          Hello, @antheds-kennel, @datatraveller1, @dr-ramaanand and All,

          @antheds-kennel, you do not even need to refer to bookmarked lines ! Just follow this road map :

          • Open your file in Notepad++

          • Open the Replace dialog ( Ctrl + H )

          • Untick all the box options

          • FIND (?-s)^(#.+\R)(.+\R?)+?(?=#|\z)

          • REPLACE $1

          • Tick the Wrap around option

          • Select the Regular expression search mode

          • Click once on the Replace All button or several times on the Replace button


          Thus, from this INPUT text :

          #STNEW:_TEST-1
          blablablabla
          #STNEW:_TEST-2
          blablablabla
          blablablabla
          blablablabla
          #STNEW:_TEST-3
          blablablabla
          #STNEW:_TEST-4
          blablablabla
          blabla
          

          You should get the expected OUTPUT text, below :

          #STNEW:_TEST-1
          #STNEW:_TEST-2
          #STNEW:_TEST-3
          #STNEW:_TEST-4
          

          @dr-ramaanand, I must admit that your regex is a nice alternative :

          FIND #STNEW:_TEST-\d+(*SKIP)(*F)|\w+

          REPLACE Leave EMPTY

          However, I think that the following syntax should be preferred :

          FIND (?-s)^#.+(*SKIP)(*F)|^.+\R?

          REPLACE Leave EMPTY


          Note that the usual generic regex, involving the SKIP and FAIL Backtracking Control verbs :

          What I dont want(*SKIP)(*F)|What I want

          Is inverted in the present example :

          What I want (*SKIP)(*F) [ Lines beginning with #] | What I don’t want [ All the other lines ]

          Best Regards,

          guy038

          D A 2 Replies Last reply Dec 29, 2024, 9:48 PM Reply Quote 1
          • D
            dr ramaanand @guy038
            last edited by dr ramaanand Dec 29, 2024, 10:10 PM Dec 29, 2024, 9:48 PM

            @guy038 Cool. \R helps find the line that is just below the line that begins with a #
            @Antheds-Kennel if your next line also has a # somewhere, you should use (#STNEW:_TEST-\d+)(*SKIP)(*F)|^.+\R? in the Find field and leave the Replace field empty and hit Replace All or Replace in files if you need to do this in multiple files of a folder

            1 Reply Last reply Reply Quote 0
            • A
              Antheds Kennel
              last edited by Antheds Kennel Dec 30, 2024, 9:22 AM Dec 30, 2024, 9:21 AM

              WOW, so many answers:) I’ve never asked on this forum before, so I became a member yesterday and took a chance on getting help. I NEVER thought I’d get so many good answers and so quickly. THANK YOU all, I’ll try this as soon as I get home. And thanks Admin for correcting my post, now I know what to do next time. Happy New Year if I don’t have time to come back to you with how it goes(because I will, I promise). Kind regards Thore

              1 Reply Last reply Reply Quote 1
              • A
                Antheds Kennel @datatraveller1
                last edited by Dec 30, 2024, 10:01 AM

                @datatraveller1 Thanks, but unfortunately I can’t, because some rows have more rows that I want to save. Sorry I missed that in my request. Thanks, Thore

                1 Reply Last reply Reply Quote 1
                • A
                  Antheds Kennel @guy038
                  last edited by Dec 30, 2024, 10:24 AM

                  @guy038 YES, this solved it. THANK YOU SO MUCH for the help. I’ll just run the large file of about 200,000 lines and see if it works there too. Have a good time, Thore.

                  1 Reply Last reply Reply Quote 1
                  • A
                    Antheds Kennel
                    last edited by Dec 30, 2024, 10:36 AM

                    Unfortunately my friends, none of your ideas worked. They either delete ALL lines without # or delete the other lines that I wanted to keep. I have to take responsibility for this mistake as I have realized that I was unclear in my request. When I have bookmarked the lines I want to delete, for example if I want to keep the lines with odd numbers in my example, I will delete the line below them. Hope you are with me now as I have a terrible time conveying my thoughts.

                    1 Reply Last reply Reply Quote 0
                    • G
                      guy038
                      last edited by guy038 Dec 30, 2024, 11:01 AM Dec 30, 2024, 10:55 AM

                      Hi, @antheds-kennel, @datatraveller1, @dr-ramaanand and All,

                      OK… So, let’s suppose that your INPUT text is :

                      Line 1
                      Line 2
                      Line 3
                      Line 4
                      Line 5
                      Line 6
                      Line 7
                      Line 8
                      ...
                      

                      You would like this OUTPUT, wouldn’t you ?

                      Line 1
                      Line 3
                      Line 5
                      Line 7
                      ...
                      

                      If this correct, use the method that I gave in the first part of my previous post, regarding the search/replacement and, simply, change the FIND and REPLACE zones by :

                      FIND (?-s)^(.+\R).+\R?

                      REPLACE $1

                      Do not forget to tick the Wrap around option and to select the Regular expression search mode !

                      BR

                      guy038

                      D 1 Reply Last reply Dec 30, 2024, 1:02 PM Reply Quote 0
                      • D
                        dr ramaanand @guy038
                        last edited by dr ramaanand Dec 30, 2024, 1:21 PM Dec 30, 2024, 1:02 PM

                        @Antheds-Kennel Please post both your exact input and output, then we can give you a perfect regular expression to get what you want. Please don’t forget to use the </> button (you can see that above the box that you type in, on the line that begins with “B I H …” and so on) to mark example text as “code” so that characters don’t get changed by the forum

                        D 1 Reply Last reply Dec 30, 2024, 1:29 PM Reply Quote 0
                        • D
                          dr ramaanand @dr ramaanand
                          last edited by dr ramaanand Dec 30, 2024, 3:31 PM Dec 30, 2024, 1:29 PM

                          @Antheds-Kennel My guess right now is to use (#STNEW:_TEST-\d+.*?)\R? in the Find field with $1 in the Replace field with the Regular expression mode ticked/selected. That will get rid of the line just below the line beginning with #STNEW:_TEST- but you must let us know what other lines there are in your original file which you want to keep (or else they will also get deleted)

                          D 1 Reply Last reply Dec 30, 2024, 4:30 PM Reply Quote 0
                          • D
                            dr ramaanand @dr ramaanand
                            last edited by dr ramaanand Dec 30, 2024, 4:45 PM Dec 30, 2024, 4:30 PM

                            @Antheds-Kennel To find just just one line after the #STNEW:_TEST- string, type #STNEW:_TEST-\d+\R\K(?:.*.*) in the Find field and then leave the Replace field blank, tick/select the Regular expression mode and hit Replace All or Replace in files if you want to do this in multiple files to get rid of it (that will get rid of only one line, just below the line beginning with #STNEW:_TEST-)

                            D 2 Replies Last reply Dec 30, 2024, 7:05 PM Reply Quote 0
                            • A
                              Antheds Kennel
                              last edited by Antheds Kennel Dec 30, 2024, 5:57 PM Dec 30, 2024, 5:52 PM

                              @guy038 said in Delete every line below a bookmarked line.:

                              $1

                              Hello again, I have uploaded a picture showing before and after with the code you gave first.
                              https://ibb.co/W51vDPT

                              P 2 Replies Last reply Dec 30, 2024, 7:00 PM Reply Quote 0
                              • G
                                guy038
                                last edited by guy038 Dec 30, 2024, 6:28 PM Dec 30, 2024, 6:15 PM

                                Hello, @antheds-kennel ,

                                Globally, my regex S/R simply keep all the odd lines et delete all the even lines !

                                But, looking at your picture, I’m wondering :

                                May be, you want to delete the lines 2, 6 and 10, only ? So, each line which is right after a bookmarked line, throughout the end of file ?

                                BR

                                guy038

                                A 1 Reply Last reply Dec 31, 2024, 10:02 AM Reply Quote 0
                                • P
                                  PeterJones @Antheds Kennel
                                  last edited by Dec 30, 2024, 7:00 PM

                                  @Antheds-Kennel said in Delete every line below a bookmarked line.:

                                  Hello again, I have uploaded a picture showing before and after with the code you gave first

                                  You can just paste pictures into your reply, so they are embedded without people having to click a link to an external site.

                                  And with some external sites, you can use the ![](...) syntax to embed the external image itself, rather than linking to their advertisement-ridden landing page
                                  Using the code ![](https://i.ibb.co/zxwV87k/33.jpg) will embed your external image:

                                  But since external sites can change their rules (thousands of imgur-hosted external images will no longer show in the forum, even though they used to work here, so much of our discussion history now has broken images because of the external site outside of our control), so it’s really best to just paste your image directly in your post.

                                  A 1 Reply Last reply Dec 31, 2024, 10:06 AM Reply Quote 1
                                  • D
                                    dr ramaanand @dr ramaanand
                                    last edited by dr ramaanand Dec 30, 2024, 7:07 PM Dec 30, 2024, 7:05 PM

                                    @dr-ramaanand said in Delete every line below a bookmarked line.:

                                    #STNEW:_TEST-\d+\R\K(?:..)

                                    @Antheds-Kennel The above Regular expression will help find it. Type #STNEW:_TEST-\d+\R\K(?:.*.*) in the Find field and then leave the Replace field blank, tick/select the Regular expression mode and hit Replace All or Replace in files if you want to do this in multiple files to get rid of it (that will get rid of only one line, just below the line beginning with #STNEW:_TEST-)

                                    1 Reply Last reply Reply Quote 0
                                    • P
                                      PeterJones @Antheds Kennel
                                      last edited by PeterJones Dec 30, 2024, 9:03 PM Dec 30, 2024, 7:08 PM

                                      @Antheds-Kennel ,

                                      After looking at your image, it is showing that the regex deletes all the lines after #STNEW: lines, and it seems you might be trying to say “but I only wanted it to delete the ones after a bookmark, so lines 2, 6, and 10 only, without also deleting 4, 8, 12, and 14”? Because if so, that is something you cannot do only with regex, because the regex engine cannot see your bookmarks. You CANNOT bookmark lines 1, 5, and 9, and then say “delete the lines right after a bookmark”.

                                      The easiest thing to do would be to bookmark the lines you actually want to delete, rather than the line before the one you want to delete, and then use Search > Bookmark > Remove Bookmarked Lines. Or, simpler, if you’re using the Mark dialog to bookmark the lines to begin with (as you show in your screenshot), then just do a search/replace regex to do the search-and-delete in one step: Something like FIND = #STNEW:_TEST-(1|3|5)\R(.*\R) REPLACE = $2

                                      –
                                      Update: that was the wrong results; FIND = (#STNEW:_TEST-(?:1|3|5)\R)(.*\R) REPLACE = $1 is, I think, what I meant (untested, as I away from NPP)

                                      A 1 Reply Last reply Dec 31, 2024, 10:10 AM Reply Quote 0
                                      • G
                                        guy038
                                        last edited by guy038 Dec 30, 2024, 8:46 PM Dec 30, 2024, 8:45 PM

                                        Hi, @antheds-kennel, @datatraveller1, @dr-ramaanand, @peterjones and All,

                                        Peter, you said :

                                        … Because if so, that is something you cannot do only with regex, because the regex engine cannot see your bookmarks. You CANNOT bookmark lines 1, 5, and 9, and then say “delete the lines right after a bookmark”.

                                        I agree with this statement ! Regexes and bookmarks are different features ! However, as the OP bookmarked the lines ending with digits 1|3|5, we can imagine this regex S/R :

                                        FIND ([135]\R).+\R

                                        REPLACE $1

                                        As expected, it would delete the lines 2, 6 and 10, only, leaving all the other lines unchanged !

                                        Best Regards

                                        guy038

                                        1 Reply Last reply Reply Quote 0
                                        • D
                                          dr ramaanand @dr ramaanand
                                          last edited by dr ramaanand Dec 31, 2024, 3:06 AM Dec 31, 2024, 2:54 AM

                                          Yes, that works, unless thr OP wants to delete other lines or skip deleting other lines. See https://regex101.com/r/EjNI9Y/1

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