Community
    • 登入

    Select/mark all lines which contain a certain pattern?

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    27 貼文 8 Posters 98.6k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • Matt AdamsM
      Matt Adams
      最後由 編輯

      Assume I have a very long text file with more than 4000 lines.

      Now I want to mark all lines which contain the pattern “foobar”.
      Afterwards I want to delete these lines WITH one (or two) clicks.

      Obviously I don’t want to do this step by step for approx 800 lines.

      How can I achieve this?

      Matt

      Claudia FrankC 1 條回覆 最後回覆 回覆 引用 0
      • Claudia FrankC
        Claudia Frank @Matt Adams
        最後由 編輯

        @Matt-Adams

        within find dialog use Mark tab and select bookmark line.
        Press Mark All button.
        Press Search->Bookmark->Romove Bookmarked Lines

        Cheers
        Claudia

        Scott SumnerS Steve RothmanS 2 條回覆 最後回覆 回覆 引用 1
        • Scott SumnerS
          Scott Sumner @Claudia Frank
          最後由 編輯

          @Claudia-Frank said:

          use Mark tab and select bookmark line

          Claudia is right except (in her very reasonable mind) she wants the checkbox for bookmarking lines with hits to be captioned “Bookmark lines”. In reality it says “Mark line”.

          A related pet-peeve of mine is that pressing the Clear button clears bookmarks even if the Mark line checkbox isn’t ticked…Grrrrrrrrrr…there is no reasonable way to clear redmarking without getting bookmarks cleared as well.

          Scott SumnerS Claudia FrankC 2 條回覆 最後回覆 回覆 引用 0
          • Scott SumnerS
            Scott Sumner @Scott Sumner
            最後由 編輯

            @Scott-Sumner said:

            no reasonable way to clear redmarking without getting bookmarks cleared

            Restarting Notepad++ (with session remembered) will do this but I don’t find that a reasonable solution! :-)

            1 條回覆 最後回覆 回覆 引用 0
            • Claudia FrankC
              Claudia Frank @Scott Sumner
              最後由 編輯

              @Scott-Sumner

              Hi Scott,

              you know that there is this type of thing that you read something which isn’t really there :-)
              and because you know how to solve it it is even more clear to the reader that it is what you read !?
              :-D

              Cheers
              Claudia

              Scott SumnerS 1 條回覆 最後回覆 回覆 引用 1
              • Scott SumnerS
                Scott Sumner @Claudia Frank
                最後由 編輯

                @Claudia-Frank

                Yes I know do know what you mean! :-D

                I should also keep in mind that @donho is from France (I think?) and is likely not a native English speaker(?) so perhaps “Mark line” to him reads like “Bookmark line” to me!

                But luckily the need to support multiple languages in Notepad++ was recognized early on…and in support of that, runtime changes to the text of the GUI are necessary…AND I can exploit that to put whatever text I want on the Mark dialog’s controls. Here’s what mine looks like, quite customized:

                Imgur

                (I know that you know this Claudia, this is for the benefit of other readers)

                1 條回覆 最後回覆 回覆 引用 3
                • PeterJonesP
                  PeterJones
                  最後由 編輯

                  +1 for don't-use replacing Extended :-)

                  1 條回覆 最後回覆 回覆 引用 2
                  • Matt AdamsM
                    Matt Adams
                    最後由 編輯

                    Thank you for suggestion (esp.Claudia Frank).

                    However this bookmark gimmick seems to me an odd workaround.

                    The correct way would be to add a checkbox in “Replace” tab dialog

                    [ ] select whole line which contains pattern

                    Then user should be able to enter a backspace in “Replace with” entry field and hit “Replace all” button.

                    Maybe I should setup a feature request…

                    1 條回覆 最後回覆 回覆 引用 0
                    • gerdb42G
                      gerdb42
                      最後由 編輯

                      How about a good ol’ RegEx:

                      Search for: ^.*?\bfoobar\b.*?\R
                      Replace with: <empty>

                      1 條回覆 最後回覆 回覆 引用 2
                      • guy038G
                        guy038
                        最後由 guy038 編輯

                        Hello, @gerdb42 and All,

                        Yes, gerdb42, you’re quite right. So, if you previously moved back to the very beginning of the file, use :

                        • SEARCH (?-s).*\bfoobar\b.*\R? , if you want to delete all lines, containing the word foobar

                        • SEARCH (?-s).*foobar.*\R? , if you want to delete all lines, containing the string foobar

                        • REPLACE EMPTY, for both cases


                        Notes :

                        • As usual, the (?-s) modifier syntax ensures that dot matches standard characters, only

                        • The \b syntax is a zero-length assertion, which notes :

                          • The boundary between a non-word character and a word character
                            or
                          • The boundary between a word character and a non-word character
                        • I added the exclamation mark, placed after the \R syntax, just in case that the string or word foobar would belong to a last line of a file, without any line break :-)

                        Best Regards,

                        guy038

                        Scott SumnerS 1 條回覆 最後回覆 回覆 引用 0
                        • Scott SumnerS
                          Scott Sumner @guy038
                          最後由 編輯

                          @guy038

                          I added the exclamation mark,

                          Hi Guy…is this a French-language thing, calling ? the exclamation mark?

                          English speakers call this the question mark.

                          If this was the first time I’ve seen you do this I would just think it was a simple mistake, but I’m sure I’ve seen you do it a few times now…

                          (I guess it is not a mistake if it is correct for your native language, though!)

                          :-D

                          1 條回覆 最後回覆 回覆 引用 1
                          • guy038G
                            guy038
                            最後由 guy038 編輯

                            Hi, @scott-sumner,

                            Ooooh, yes, Scoot ! Just a silly mistake. I did mean a question mark, after the \R syntax !

                            Anyway, in French the spelling is almost similar : exclamation mark <–> point d’exclamation and question mark <–> point d’interrogation !

                            Cheers,

                            guy038

                            Scott SumnerS 1 條回覆 最後回覆 回覆 引用 1
                            • Scott SumnerS
                              Scott Sumner @guy038
                              最後由 Scott Sumner 編輯

                              @guy038

                              Maybe it is best to say "I did mean a ?, after the \R syntax…this removes all doubt about what one is saying. :-)
                              I try to do this in postings, and also try to refer to commands by their “menu path” rather than their default Shortcut Mapper keys–because the keys may be gibberish to someone (like me) that has extensively remapped the defaults. I started remapping the instant I saw Replace defaults to ctrl+h…

                              Regarding:

                              Scoot

                              Okay, so this may be a favorite typo of yours as well!
                              Who is “Scoot”?
                              Kind regards,
                              Sco{1}t
                              :-D

                              Scott SumnerS 1 條回覆 最後回覆 回覆 引用 1
                              • Scott SumnerS
                                Scott Sumner @Scott Sumner
                                最後由 編輯

                                @Scott-Sumner

                                Darn it,
                                Sco{1}t{2}

                                1 條回覆 最後回覆 回覆 引用 1
                                • Perry SticcaP
                                  Perry Sticca
                                  最後由 編輯

                                  Rather than start a new thread, I’ll ask a very similar question in this one:
                                  I’d like to mark, sort and then delete all lines that end with:

                                  ;0

                                  (That’s a semicolon and a zero. A semicolon and a zero might occur elsewhere in the line, but that’s OK - I only care about if they occur at the end of each line.)

                                  I’d like to first sort them to the beginning or end of the file, so I can take a look at them, but then I’d like to delete them. What’s the best way to accomplish this?

                                  gerdb42G 1 條回覆 最後回覆 回覆 引用 0
                                  • guy038G
                                    guy038
                                    最後由 guy038 編輯

                                    Hello @perry-sticca and All,

                                    The more simple way, what I’m thinking of, would be :

                                    • Move back to the very beginning of your file ( Ctrl + Home )

                                    • Choose the menu choice Search > Mark…

                                    • Tick the option Bookmark line and, may be, the Purge for each search option

                                    • Select the Regular expression search mode

                                    • Type the regex ;0$, in the Find what line

                                    • Click on the Mark All button

                                    => All the specific lines, ending with the string ;0 are highlighted and bookmarked !

                                    • Choose the menu choice Search > Bookmark > Cut Bookmarked Lines

                                    • Move to the very end of your file ( Ctrl + End )

                                    • You may insert a short line separator

                                    • Paste, with Ctrl + V, all the lines ending by the string ;0

                                    • After having a look at them…, move back to your line separator

                                    • Hit the Ctrl + Shift + End shortcut, to select all these specific lines

                                    • Delete them by hitting the Suppr key

                                    Et voilà !

                                    Best Regards,

                                    guy038

                                    1 條回覆 最後回覆 回覆 引用 2
                                    • gerdb42G
                                      gerdb42 @Perry Sticca
                                      最後由 編輯

                                      @Perry-Sticca

                                      If you want to delete the lines anyway, why mark and sort them first? To delete them right away, search for (?-s)^.*?;0(?:\R|$) and replace with <empty>.

                                      Scott SumnerS 1 條回覆 最後回覆 回覆 引用 0
                                      • Scott SumnerS
                                        Scott Sumner @gerdb42
                                        最後由 編輯

                                        @gerdb42

                                        Sometimes you just wanna be sure you are doing the right thing…related maybe…

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • Scott SumnerS
                                          Scott Sumner
                                          最後由 編輯

                                          A couple of notes on @guy038’s response:

                                          • I might change the regex to (?-s)^.+?;0$ as that will redmark the entire line for matches (easier for the visual inspection that @perry-sticca wants to do)
                                          • When I use the Mark feature 99.5%(?) of the time I want it to affect the whole file without having to move the caret to the top first; thus I suggest ticking the Wrap around checkbox rather than the action “Move back to the very beginning of your file (Ctrl + Home)”. Either way works, though.

                                          @guy038, what is the Suppr key? Presume you mean the more-commonly labeled Delete key…

                                          ----- New spin on a solution:-----

                                          Maybe more aligned with @perry-sticca 's original desire would be to do this Replace-All (not a Mark) operation that puts a “high-valued” character at the start of the wanted-to-be-matched lines:

                                          Find what zone: (?-s)^(?=.*?;0$)
                                          Replace with zone: \x7f
                                          Wrap around checkbox: ticked
                                          Search mode radio-button: Regular expression

                                          Post replacement, do a Edit (menu) -> Line Operations -> Sort Lines ______ to get the desired lines grouped together at one end of the file.

                                          From there, once confirmed by visual inspection that the lines should really be removed, it is a simple matter to manually do so. Of course, if any are to be retained after this inspection it would be smart to remove the leading \x7f character from each!

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • guy038G
                                            guy038
                                            最後由 guy038 編輯

                                            Hi, scott-sumner,

                                            Firstly, you’re right about the key name : the keys Insert and Delete, of an American / English keyboard, are named Inser and Suppr, on a French keyboard !


                                            Secondly, I didn’t think about sorting, Nice idea too ! However, there’s a small difference : the block of lines, ending with ;0, moved at end of file, keeps its initial order with the bookmarks solution, whereas it’s sorted with the \x7F character and sort solution !


                                            Thirdly, concerning my preference, to move back, first, at beginning of file, before a regex S/R, rather than ticking the Wrap around option, it’s just because, I think that, in particular cases, it could cause different results, according to the initial location of the caret, inside the file.

                                            So, I tried to find out a regex, which will demonstrate this fact ! After a while, I came across this one :

                                            SEARCH (?-s)^abc\R(?s).*abc\z

                                            REPLACE EMPTY

                                            and I tested it, against the example text, below, in a NEW tab :

                                            Line 01
                                            abc
                                            Line 02
                                            Line 03
                                            abc
                                            Line 04
                                            --><--
                                            Line 05
                                            abc
                                            Line 06
                                            Line 07
                                            abc
                                            

                                            IMPORTANT : The last line abc, of this text, does NOT contain any line break and physically ends the new tab contents !

                                            Then :

                                            • Put the caret between the two arrows, in the middle of the text.

                                            • Check the Wrap around option

                                            • Click on the Find Next button

                                            => The following block of text is selected :

                                            abc
                                            Line 06
                                            Line 07
                                            abc
                                            
                                            • Now, click on the Replace button ( Not the Replace All one ! )

                                            => This block is, of course, deleted and it remains :

                                            Line 01
                                            abc
                                            Line 02
                                            Line 03
                                            abc
                                            Line 04
                                            --><--
                                            Line 05
                                            

                                            You get the message Replace: 1 occurrence was replaced. The next occurrence not found. This is logical as, when the process wraps around and moves back to beginning of text, to go on searching, it cannot find, again, a string abc at the very end of the new tab ( part abc\z )


                                            Now, let’s redo the same steps and use, instead, the Replace All button :

                                            • Get the original text, by an undo action ( Ctrl + Y )

                                            • Put, again, the caret between the two arrows, in the middle of the text.

                                            • Verify that the Wrap around option is still checked

                                            • Click on the Replace All action

                                            => Surprisingly, this time, almost all text is deleted and it remains the unique line :

                                            Line 01
                                            

                                            In other words, it looks like the process moved back, first, at beginning of file, selected the block between the first string abc and the last string abc ( the last line ) and, finally, deleted it !!

                                            Logically, if process had begun at caret position and had moved back to beginning of file, it should had deleted, only, the following block of text, as the ending string abc does not exist anymore, due to the first replacement !!??

                                            abc
                                            Line 06
                                            Line 07
                                            abc
                                            

                                            Consequently, it seems that, when the Regular expression search mode and the wrap around option are, both, ticked, a click on the Replace All button performs the regex S/R from the very beginning of the file till its very end, EVEN IF the initial caret position was anywhere inside the file contents !

                                            If this assertion is exact, we would better to always tick the Wrap around option, before a full regex S/R:-) Could you investigate, a bit, about it, may be from source code ? Many thanks, Scott, by advance !

                                            Cheers,

                                            guy038

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 第一個貼文
                                              最後的貼文
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors