Community
    • Login

    Search Just Within Provided Line Number

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 6 Posters 482 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.
    • CoisesC
      Coises @Scott Daniels
      last edited by Coises

      @Scott-Daniels said in Search Just Within Provided Line Number:

      I need to be search line number 4 for the number 2. I need to perform this search on a directory of text files. Is this possible?

      Search | Find in Files… from the main menu; then enter:

      Find what: \A([^\r\n]*+\R){3}[^\r\n]*\K2
      Search Mode: Regular expression
      Filters: *.* or *.txt
      Directory: set as appropriate using … button

      and click Find All.

      Edit to add: Rereading, I see that you said “the number 2” and not “the digit 2.” If you meant that you specifically need to find 2 but not 20 or 12, then change the expression to:

      Find what: \A([^\r\n]*+\R){3}[^\r\n]*\K(?<!\d)2(?!\d)

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

        Hello, @scott-daniels, @terry-r, @coises and All,

        @scott-daniels, I realize that other people have already beaten me to it !!


        Here is how I understand your question :

        • For each text file in a specific folder :

          • Don(t care about the first three lines of each file

          • And looks for, at least, one occurrence of the string 2 in the fourth line of each file

        Am I right regarding this assumption ?


        In case of a positive answer, here is my solution, similar to their ones :

        • Open the Find in Files dialog ( Ctrl + Shift + F )

        • Untick all box options

        • Fill in the string (?-s)\A(.*\R){3}.*?\K2 in the Find what: zone

        • Leave the Replace with: zone Empty

        • Enter *.txt in the Filters : zone

        • Enter full pathname of your directory name in the Directory : zone

        • Select the Regular expression search mode

        • Click once on the Find All button

        • Wait till the Search results window is displayed !

        Best regards

        guy038

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

          I think @Coises 's answer is best here:

          • Terry didn’t use \A to keep the search anchored to the start of file
          • Guy did too much (OP didn’t mention deleting the matching text, only finding it)
          • Coises gets bonus points for using the possessive form
          Terry RT 1 Reply Last reply Reply Quote 1
          • Terry RT
            Terry R @Alan Kilborn
            last edited by Terry R

            @Alan-Kilborn said in Search Just Within Provided Line Number:

            Terry didn’t use \A to keep the search anchored to the start of file

            Unless I’m mistaken, doesn’t each Find in Files search always start at the beginning, thus making the \A redundant. Of course keeping it in just means being consistent with regex making, but since it’s used very in-often we hardly ever see it in regexes even for opened documents.

            Terry

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

              @Terry-R said in Search Just Within Provided Line Number:

              doesn’t each Find in Files search always start at the beginning

              Yes, but nothing prevents your regex from starting a match later in the file (if one/more happens to exist). The \A will do this.

              Of course, since OP is apparently just visually inspecting results, he could ignore any matches that aren’t near the top-of-file.

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

                @Alan-Kilborn said in Search Just Within Provided Line Number:

                Yes, but nothing prevents your regex from starting a match later in the file (if one/more happens to exist).

                Isn’t that a contradiction?

                First you agree that each search in “Find in Files” commences at the start of a file. Then you suggest somehow a regex can “slip” to another possible occurrence. I don’t see how. Even if the search is repeated it will still find the same occurrence as the previous search.

                Terry

                PS maybe if there is some further debate we should start a new thread rather than cloud the OP’s judgement on other things.

                PeterJonesP 1 Reply Last reply Reply Quote 0
                • PeterJonesP
                  PeterJones @Terry R
                  last edited by PeterJones

                  @Terry-R ,

                  As with all regex, if it doesn’t match at the start of the search, it keeps going to the end, checking if each position matches. Your regex will match the file below, even though the fourth line doesn’t have a 2 in it anywhere – lines 2-5 fit the pattern you defined.

                  one
                  two
                  three
                  four
                  five has 2
                  six
                  

                  @Alan-Kilborn ,

                  I am not sure @Coises is right, either, because his assumes the fourth line starts with a 2, whereas the phrasing of the OP implies it might be anywhere on line 4. (And @guy038 replicated that understanding.)

                  (?-s)\A(.*\R){3}\K(.*2.*) is my suggestion – I also removed the trailing \R, otherwise a 4-line file missing final newline wouldn’t be found.

                  Alan KilbornA Terry RT 2 Replies Last reply Reply Quote 0
                  • Alan KilbornA
                    Alan Kilborn @PeterJones
                    last edited by Alan Kilborn

                    @PeterJones said in Search Just Within Provided Line Number:

                    I am not sure @Coises is right, either, because his assumes the fourth line starts with a 2

                    Coises uses [^\r\n]*\K2 which would start trying to match at the beginning of line 4 (from the earlier stuff in his regex). It appears to read (starting there) as "match any non-line-ending chars zero-or-more times followed by a 2 – seems to fit the bill. I’m away from my PC right now, so I can’t try it, though.

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

                      @Alan-Kilborn ,

                      Oh, you’re right… I missed the [^\r\n]* when I was reading it (and didn’t try his before my post).

                      So yes, I just confirmed @Coises’ suggestion works on my test data, too.

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

                        @PeterJones said in Search Just Within Provided Line Number:

                        Your regex will match the file below, even though the fourth line doesn’t have a 2 in it anywhere – lines 2-5 fit the pattern you defined.

                        Thanks, I now stand corrected. Somehow with my meagre testing on a small file I neglected to include another occurrence further down the file. Of course that shouldn’t have affected my decision as I knew the regex completes as many iterations as necessary to include the “whole file”, but there was a snafu in my solution. Yes using the \A is necessary to JUST look at the nth line ONLY.

                        Thanks
                        Terry

                        PS @Alan-Kilborn I DID read your info, but somehow that didn’t allow me to make the connection and Peter’s example is what it took to get me over the line. I guess today just isn’t my day, there’s always tomorrow!

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

                          Hello, @scott-daniels, @terry-r, @coises @alan-kilborn, @peterjones and All,

                          @alan-kilborn, you said :

                          • Guy did too much (OP didn’t mention deleting the matching text, only finding it)

                          I don’t understand because, in my post, I do NOT speak about deleting the 2 digit, at any moment ?


                          Now, about the @coises syntax \A([^\r\n]*+\R){3}[^\r\n]*\K2, with an atomic ( possessive ) syntax, I don’t see the bonus to use it !

                          Indeed, it necessarily needs to get some EOL chars after the possible standard chars [\r\n]*. Thus, it will grasp all standard chars, even zero, followed with EOL char(s), in all cases !

                          And note that I use the (?-s) modifier which means that the . regex char is identical to [^\r\n\f], anyway !


                          But, I must admit that the second @coises’s solution is more accurate, if the OP needs to get the number 2, only

                          Thus, we end up, with this search regex :

                          (?-s)\A(?:.*\R){3}.*?\K(?<!\d)2(?!\d)

                          Best Regards,

                          guy038

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

                            @guy038 said in Search Just Within Provided Line Number:

                            I don’t understand because, in my post, I do NOT speak about deleting the 2 digit, at any moment ?

                            Ah…you’re right. I supposed I was confused because when you said:

                            Leave the Replace with: zone Empty

                            It appeared to me like this was setting up a replace-with-nothing aka delete operation!

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