Community
    • Login

    Search and extract results between two strings in multiple files

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    6 Posts 3 Posters 2.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.
    • Amit PandeyA
      Amit Pandey
      last edited by

      Hello Everyone,

      I am trying to search and extract results between two strings in multiple files.

      for example
      i need to search string “Assign” between ‘PROCEDURE INIT-RECORD’ and ‘END PROCEDURE’, something like below

      PROCEDURE INIT-RECORD

      DEFINE variable a;
      DEFINE variable b;
      DEFINE variable c;

      Assign a = 4;

      Assign b = 6;

      c = a + b;
      END PROCEDURE

      the search results should retrieve
      Assign a = 4, and
      Assign b = 6.

      and similar Assign statements across multiple files within the PROCEDURE INIT-RECORD.

      i tried NPP search using
      (?s)(?<=PROCEDURE INIT-RECORD)(.+?)(?<=END PROCEDURE)

      the above regex search string selects the target procedure but i cannot copy the text from all the results.

      Any help/pointers/ideas here will be appreciated.

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

        @Amit-Pandey said in Search and extract results between two strings in multiple files:

        Your regex seems ok, as a first step.
        But use the Mark All function followed by Copy Marked Text and paste into a new N++ tab.
        That way, in the new tab you have Assign data that only occurs between the specified delimiters; all others will not be in the new tab.
        Then you can do a similar regex Mark operation for Assign.
        I think you know where to go from there.

        search and extract results between two strings in multiple files.

        You will need to do the files one at a time, manually.

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

          Hello, @amit-pandey, @alan-kilborn and All,

          @amit-pandey, I suppose your need this regex technique

          If we adapt this generic regex to your specific problem, we end with that functional regex :

          MARK (?-i:PROCEDURE INIT-RECORD|(?!\A)\G)(?s-i:(?!END PROCEDURE).)*?\K(?-si:Assign.+)


          So :

          • Open your file in Notepad++

          • Move the cursor to its very beginning

          • Open the Mark dialog ( Ctrl+ M )

          • Untick ALL box options

          • Check the 3 options Bookmark line, Purge for each serch and Wrap around

          • Select the Regular expression search mode

          • Click on the MArk All button

          • Click on the Copy Marked Text button

          • Open a new tab ( Ctrl + N )

          • Paste The Mark contents ( Ctrl + V )

          Here you are !

          Best Regards

          guy038

          Amit PandeyA 1 Reply Last reply Reply Quote 2
          • guy038G guy038 referenced this topic on
          • Amit PandeyA
            Amit Pandey @guy038
            last edited by

            Hello @guy038 , @Alan-Kilborn

            Thanks for your time in looking into this.

            @Alan-Kilborn , the file volume is huge hence working this out for one file at a time is a challenge.

            @guy038 , the regex search string did the trick, i used it in ‘Find in Files’ option.
            The search results are giving me the first instance of the target string within the target procedure.
            The results are far much better than what i was getting earlier with my search efforts.

            Although I still cannot get all the search results but i got a good perspective on my search results

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

              Hi, @amit-pandey, @alan-kilborn and All,

              @amit-pandey, you said :

              Although I still cannot get all the search results but i got a good perspective on my search results

              This means that your initial example is totally insufficient and that we need some other blocks PROCEDURE INIT-RECORD ....... END PROCEDURE that allow us to build the appropriate regex which will match all the expected results !

              So, just add a longer exemple, which describe most of the cases which may occur, using the </> function, in your answer !

              BR

              guy038

              Amit PandeyA 1 Reply Last reply Reply Quote 0
              • Amit PandeyA
                Amit Pandey @guy038
                last edited by

                Hello @guy038 ,

                My bad, i misinterpreted your reply and went into a different direction.
                (also had to upgrade my NPP (upgraded to v8.4.9) to reflect ‘Copy Marked text’ option".

                I have now achieved what is required.

                Here are the steps which i followed

                • Search for Regex String ‘PROCEDURE INIT-RECORD|ASSIGN|END PROCEDURE’ across the files
                • Copied the search result in a new window
                • Applied regex search string provided by you in the earlier reply’ and followed the provided steps
                • All the target keywords within the target procedure were marked
                • Used ‘Copy Marked Text’ option and got my results

                Thanks a lot for your help.

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