Community
    • Login

    Finding and deleting a series of lines

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    15 Posts 5 Posters 4.7k 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.
    • JamesJ
      James
      last edited by

      So I have to edit a large html file, where I’d like to search for a term and delete a certain number of lines above and below, like in the example below.

      (text)
      1234
      (text)

      I’d also like to know if it’s possible to use notepad++ in a cli in some way so I can automate these edits, as doing it manually would take hours or days.

      Thanks in advance.

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

        Hello @james and All,

        To search and delete some non-empty lines before and after a specific line, containing the expression to search for, use the generic S/R, below :

        SEARCH (?-is)(^.+\R){n}.*Expression.*\R(?1){m}

        REPLACE Leave EMPTY

        where n represents the number of lines before the line containing Expression ( n >= 0 )

        and m represents the number of lines after the line containing Expression ( m >= 0 )

        For instance, from your example :

        • The regex (?-is)^(.+\R){3}.*1234.*\R(?1){0} would search for the 3 lines before a line containing the number 1234 and 0 line after and replace all this block of lines with the line containing the string 1234 only

        • The regex (?-is)^(.+\R){1}.*1234.*\R(?1){4} would search for one line before any line containing the number 1234 and four lines after and replace all this block of lines with the line 1234 only

        Best regards,

        guy038

        1 Reply Last reply Reply Quote 2
        • JamesJ
          James
          last edited by

          How am I supposed to input those commands in notepad++? There’s no cli interface that I know of, and there doesn’t seem to be a place in the gui version. Unless there’s a guide you could share, that would be great. Thanks

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

            @James

            It would be a manual entry type of thing, with the affected places shown in yellow:

            1ae69dc8-ffd0-448f-bf8f-37e399790efd-image.png

            For automation, you’d have to turn to a scripting plugin, I suppose, if Replace in Files (see the Find in Files tab of the above shown window) doesn’t meet your need.

            1 Reply Last reply Reply Quote 1
            • JamesJ
              James
              last edited by

              Now it’s saying there’s no occurances of that command ((?-is)(^.+\R){n}.Expression.\R(?1){m}). I assume it’s because it’s searching for it as a string, not as a command. How would I input the command from the earlier message to do this? Thank you for your help

              Alan KilbornA 2 Replies Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @James
                last edited by Alan Kilborn

                @James

                it’s saying there’s no occurances of that command ((?-is)(^.+\R){n}.Expression.\R(?1){m}).

                Well, hopefully you understand that this is a formula and not something you would directly input:

                ddc4710f-c4c0-45ea-b302-d868731ecd36-image.png

                This, however, is an application of that formula, and thus could be directly entered into the Find what box:

                (?-is)^(.+\R){1}.*1234.*\R(?1){4}

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

                  @James said in Finding and deleting a series of lines:

                  I assume it’s because it’s searching for it as a string, not as a command.

                  I’m confused by this part.
                  There is no searching by command, there is only searching by string.

                  Alan KilbornA 1 Reply Last reply Reply Quote 1
                  • JamesJ
                    James
                    last edited by

                    I should’ve phrased that better. When I try inputting that command in the find what section, it seems to be searching for it as a string, rather than as some sort of formula like you said.

                    Terry RT 1 Reply Last reply Reply Quote 0
                    • Terry RT
                      Terry R @James
                      last edited by

                      @James said in Finding and deleting a series of lines:

                      it seems to be searching for it as a string, rather than as some sort of formula like you said.

                      This is a regular expression so the search mode MUST be regular expression. That was the 3rd highlight in the image @Alan-Kilborn provided above.

                      Terry

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

                        @Alan-Kilborn said in Finding and deleting a series of lines:

                        There is no searching by command, there is only searching by string.

                        I suppose that statement I made was misleading, as a “regular expression” rather is more like a formula/command than it is like a string. Sorry for any confusion I brought. :-)

                        1 Reply Last reply Reply Quote 1
                        • JamesJ
                          James
                          last edited by

                          Well even when I use the find what section and regular expression options, it seems to be searching for it as a string regardless. 56fe8fb2-fc01-4025-99e4-eebc8649d599-image.png 0e93ee55-9189-472e-9899-ef3d08970ab2-image.png

                          Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 1
                          • Alan KilbornA
                            Alan Kilborn @James
                            last edited by

                            @James

                            Hmm. Yea, I see what you mean. I was using the formula to try to find the text “copy” in the N++ license.txt file. Even though I tweaked the formula a bit so that it would ignore case, I was getting no hits.

                            As @guy038 wrote the formula, I’ll let him comment on what might not quite be right there.

                            1 Reply Last reply Reply Quote 1
                            • PeterJonesP
                              PeterJones @James
                              last edited by PeterJones

                              @James ,

                              It works on a single file for me.

                              ea3f2aad-bd2c-4c05-baea-8896d4ca3eac-image.png

                              eight
                              seven
                              six
                              five
                              four
                              three
                              two
                              one
                              1234
                              one
                              two
                              three
                              four
                              five
                              six
                              seven
                              eight
                              nine
                              ten
                              eleven
                              twelve
                              thirteen
                              fourteen
                              fifteen
                              

                              FIND = (?-is)^(.+\R){7}.*1234.*\R(?1){14}

                              Taking @Alan-Kilborn’s example, I see the problem. The (.+\R) assumes there’s at least one character on every line before or after the matching text. The license.txt has blank lines throughout, so there usually aren’t many lines before and after. So change to (.*\R) => FIND = (?i-s)^(.*\R){7}.*copy.*\R(?1){14} (I also changed to (?i-s) so that the copy was case-insensitive, as Alan mentioned.)

                              0dcfda87-bb8f-43e5-b358-64696bcd69e1-image.png

                              So if your real data has any lines that are blank (just newlines), you will need to use .* instead of .+

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

                                Hello @james, @lan-kilborn, @terry-r, @peterjones and All,

                                @james : Thanks to @peterjones, you have the right solution !

                                So, in summary :

                                • To delete some non-empty lines before and/or after a line, containing Expression, with this exact case :

                                  • SEARCH (?-is)(^.+\R){n}.*Expression.*\R(?1){m}

                                • To delete some non-empty lines before and/or after a line, containing Expression, whatever its case

                                  • SEARCH (?i-s)(^.+\R){n}.*Expression.*\R(?1){m}

                                • To delete some lines, possibly empty, before and/or after a line, containing Expression, with this exact case :

                                  • SEARCH (?-is)(^.*\R){n}.*Expression.*\R(?1){m}

                                • To delete some lines, possibly empty, before and/or after a line, containing Expression, whatever its case

                                  • SEARCH (?i-s)(^.*\R){n}.*Expression.*\R(?1){m}

                                • where n represents the number of lines before the line containing Expression ( n >= 0 )

                                • and     m represents the number of lines after    the line containing Expression ( m >= 0 )

                                Best Regards,

                                guy038

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

                                  @guy038

                                  Actually, what you’ve specified is for finding some… , not deleting some…

                                  To delete, we need to specify:

                                  • SEARCH (as above)
                                  • REPLACE leave this field blank

                                  :-)

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