Community
    • Login

    Find text, display line with text and the next line

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 6 Posters 545 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.
    • Robert Or Janet DiebelR
      Robert Or Janet Diebel
      last edited by

      With RegEx, how would I Find text, display line with text and the next line both.
      Example:

      	Line    1: \id MRK
      	Line    4: \toc2 Mako
      

      (locate and print both lines)

      datatraveller1D Alan KilbornA 2 Replies Last reply Reply Quote 0
      • datatraveller1D
        datatraveller1 @Robert Or Janet Diebel
        last edited by

        @Robert-Or-Janet-Diebel Maybe “Find All in Current Document” is what you are looking for?

        1 Reply Last reply Reply Quote 0
        • Alan KilbornA
          Alan Kilborn @Robert Or Janet Diebel
          last edited by

          @Robert-Or-Janet-Diebel said in Find text, display line with text and the next line:

          With RegEx, how would I Find text, display line with text and the next line both.

          You have not provided enough information for anyone to help you.
          Sadly, though, I’m sure some will try.

          Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 1
          • Robert Or Janet DiebelR
            Robert Or Janet Diebel @Alan Kilborn
            last edited by Robert Or Janet Diebel

            @Alan-Kilborn
            I wish to search a file for a line containing a pattern.
            On finding it I wish to display that line, and the line following.
            The file looks like this:

            Search "\\id\s.{1,3}|\\toc2\s.*|\\toc3\s.*" (96 hits in 32 files of 33 searched) [RegEx: Case]
              E:\Documents\Zambia\41MATLUV.SFM (3 hits)
            	Line    1: \id MAT 41MATLUVSF3, August 1, 2011, LCP LUVALE ST4 Chavuma, Zambia
            	Line    4: \toc2 Mateu
            	Line    5: \toc3 Mat
              E:\Documents\Zambia\42MRKLUV.SFM (3 hits)
            	***Line    1: \id MRK
            	Line    4: \toc2 Mako***
            	Line    5: \toc3 Mako
              E:\Documents\Zambia\43LUKLUV.SFM (3 hits)
            	Line    1: \id LUK 43LUKLUVSF1, August 1, 2011, LCP LUVALE ST4 Chavuma, Zambia
            	Line    4: \toc2 Luka
            	Line    5: \toc3 Luka
              E:\Documents\Zambia\44JHNLUV.SFM (3 hits)
            	Line    1: \id JHN
            	Line    4: \toc2 Yowano
            	Line    5: \toc3 Yowa
            
            

            I would search for “MRK”, and get
            Line 1: \id MRK
            Line 4: \toc2 Mako

            Just adding I found “\id\sMRK.\R\K^.\R” here which prints the second line, but not the first.

            Terry RT mkupperM 2 Replies Last reply Reply Quote 0
            • Terry RT
              Terry R @Robert Or Janet Diebel
              last edited by

              @Robert-Or-Janet-Diebel said in Find text, display line with text and the next line:

              \id\sMRK.\R\K^.\R

              In that regex you supplied the \K advises the system to disregard the string found thus far. So that is very likely why you ONLY get the 2nd line. You also appear to show a single DOT character behind the MRK. This signifies any 1 character, not sure if you intended to have only 1 character position before the end of line (\R). I would be inclined to put a .* there, so that means any number of characters but also precede the regex with (?-s) meaning DOT characters can’t include end of line/new line characters.

              Your example, whilst entered into a code box is admirable, it is only the search result. So it’s not actually the file as you stated, only the search results from finding things.

              Overall you are on the right track, but the example isn’t helpful and the post is very light on content. Maybe if you were to try again with correct example (in a code box) and explain a bit more including a 2nd code box with what you expect as a result of the 1st code box examples being searched (the result) you might get a bit more response.

              Terry

              1 Reply Last reply Reply Quote 3
              • mkupperM
                mkupper @Robert Or Janet Diebel
                last edited by mkupper

                @Robert-Or-Janet-Diebel said in Find text, display line with text and the next line:

                On finding it I wish to display that line, and the line following.

                Unfortunately, Notepad++'s Find in Files thing only shows the first line of the results in the search results window.

                For example, of you did a Find in Files search for .*abc.*\R.* then the search results will show all lines that contain abc. If you click on one of those in the search results window then Notepad++ will take you to the line in the file and with the abc, all following text on that line, and all of the line after that selected.

                This is likely not terribly useful as you mentioned wanting to print the results.

                If you were dealing with a single file then you could have done a Mark-all using .*abc.*\R.*\R and then clicked the [Copy marked text] button. That will allow you to copy the text of matches into a new file which you then print. Note that I did a couple of things to help this work better. 1) I added .* at the front of the expression to select any text on that line that appears before the abc. I also added an \R at the end so that when I do the [Copy marked text] that there is an end-of-line insert at the end of what gets loaded into the copy/paste buffer.

                You will discover that the pairs of lines are not separated by blank lines. You’d need to add those with an extra search/replace.

                Search (.*abc.*\R.*\R)
                Replace: \1\r\n

                You will also discover that it’s painful if you are dealing with many files as Notepad++'s marking thing only works on one file at a time and the Marking thing is the only one of the various ways to search or replace that offers the [Copy marked text] button that allowed for extracting the text.

                I used abc here as a generic pattern in this example. Your
                \\id\s.{1,3}|\\toc2\s.*|\\toc3\s.* will work though you should wrap that in (?:parentheses) so that we have

                Search (.*(?:\\id\s.{1,3}|\\toc2\s.*|\\toc3\s.*).*\R.*\R)

                The parentheses tell the regular expression parser that the
                \\id\s.{1,3}|\\toc2\s.*|\\toc3\s.* is a sub-expression.

                The (?:..) with the leading question/colon tells the regular expression parser to not assign a \1 or $1 group number to the sub-expression.

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

                  Hello, @robert-or-janet-diebel, @alan-kilborn, @terry-r, @mkupper and All,

                  Sorry for all people which answered to @robert-or-janet-diebel, but, this time :

                  • I first tried to understand the @robert-or-janet-diebel’s goal

                  • I did not try to analyze the other replies in order to keep a neutral point of view

                  • I tried to elaborate my own solution


                  And I concluded with this generic regex S/R :

                  MARK (?-is).*Expression\R.*\R?

                  This results in the 3 functional regex expressions below :

                  MARK (?-is).*MRK\R.*\R?

                  MARK (?-is).*Luka\R.*\R?

                  MARK (?-is).*Yowano\R.*\R?


                  Thus, given your INPUT text :

                  Search "\\id\s.{1,3}|\\toc2\s.*|\\toc3\s.*" (96 hits in 32 files of 33 searched) [RegEx: Case]
                    E:\Documents\Zambia\41MATLUV.SFM (3 hits)
                  	Line    1: \id MAT 41MATLUVSF3, August 1, 2011, LCP LUVALE ST4 Chavuma, Zambia
                  	Line    4: \toc2 Mateu
                  	Line    5: \toc3 Mat
                    E:\Documents\Zambia\42MRKLUV.SFM (3 hits)
                  	***Line    1: \id MRK
                  	Line    4: \toc2 Mako***
                  	Line    5: \toc3 Mako
                    E:\Documents\Zambia\43LUKLUV.SFM (3 hits)
                  	Line    1: \id LUK 43LUKLUVSF1, August 1, 2011, LCP LUVALE ST4 Chavuma, Zambia
                  	Line    4: \toc2 Luka
                  	Line    5: \toc3 Luka
                    E:\Documents\Zambia\44JHNLUV.SFM (3 hits)
                  	Line    1: \id JHN
                  	Line    4: \toc2 Yowano
                  	Line    5: \toc3 Yowa
                  
                  • Open the Mark dialog ( Ctrl + M )

                  • Untick all the box options

                  • FIND (?-is).*MRK\R.*\R?

                  • Check the Bookmark Line option

                  • Check the Wrap around option

                  • Select the Regular expression search mode

                  • Click once on the Mark All button

                  Now use the same method with the two other searches :

                  • FIND (?-is).*Luka\R.*\R?

                  • FIND (?-is).*Yowano\R.*\R?

                  At this point, you should have 6 lines bookmarked, as shown below ( Note the • bookmark at the beginning of these 6 lines ) :

                  Search "\\id\s.{1,3}|\\toc2\s.*|\\toc3\s.*" (96 hits in 32 files of 33 searched) [RegEx: Case]
                    E:\Documents\Zambia\41MATLUV.SFM (3 hits)
                  	Line    1: \id MAT 41MATLUVSF3, August 1, 2011, LCP LUVALE ST4 Chavuma, Zambia
                  	Line    4: \toc2 Mateu
                  	Line    5: \toc3 Mat
                    E:\Documents\Zambia\42MRKLUV.SFM (3 hits)
                  •	***Line    1: \id MRK
                  •	Line    4: \toc2 Mako***
                  	Line    5: \toc3 Mako
                    E:\Documents\Zambia\43LUKLUV.SFM (3 hits)
                  	Line    1: \id LUK 43LUKLUVSF1, August 1, 2011, LCP LUVALE ST4 Chavuma, Zambia
                  •	Line    4: \toc2 Luka
                  •	Line    5: \toc3 Luka
                    E:\Documents\Zambia\44JHNLUV.SFM (3 hits)
                  	Line    1: \id JHN
                  •	Line    4: \toc2 Yowano
                  •	Line    5: \toc3 Yowa
                  

                  Now, click on the Copy Marked Text button

                  • Open a new tab ( Ctrl + N )

                  • Paste the clipboard contents ( Ctrl + V )

                  And here your expected OUTPUT text :

                  	***Line    1: \id MRK
                  	Line    4: \toc2 Mako***
                  
                  ----
                  	Line    4: \toc2 Luka
                  	Line    5: \toc3 Luka
                  
                  ----
                  	Line    4: \toc2 Yowano
                  	Line    5: \toc3 Yowa
                  
                  

                  Don’t know if my solution will be useful to the OP. Just a try !

                  Best Regards,

                  guy038

                  Robert Or Janet DiebelR 1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @mkupper
                    last edited by

                    @mkupper said:

                    Find in Files thing

                    The OP didn’t say anything about wanting what they want over multiple files, so I think the “Mark thing” solution proposed by you and @guy038 fits the need.

                    1 Reply Last reply Reply Quote 0
                    • Robert Or Janet DiebelR
                      Robert Or Janet Diebel @guy038
                      last edited by

                      @guy038 said in Find text, display line with text and the next line:

                      (?-is).Luka\R.\R?

                      I do not take the gift of anyones time lightly.
                      Many thanx, much appreciated.
                      Most helpful.

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