Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Filter lines based on word on another line

csian
2013-08-01
2013-08-14
  • csian
    csian
    2013-08-01

    Hi there,

    having to go through debug logs quite often, I would like to be able to search for a certain keyword and then have only the third line down for any line that contains that keyword displayed. Is there any way I can do that in Notepad++?

    Thank you

     
    • udesign
      udesign
      2013-08-02

      ctrl+h opens an editing window

       
  • udesign
    udesign
    2013-08-02

     
    Last edit: udesign 2013-08-02
  • THEVENOT Guy
    THEVENOT Guy
    2013-08-04

    Hello csian,

    I've a solution, with the help of regular expressions, which allow to select a line, placed n lines, after a keyword, contained in a line above.

    I saw your post at 3.00 am, in the morning, ( in France ! ) and I'd better go to bed ! But, after the week-end, I'll post you a solution, on next Monday !

    See you soon !

    Cheers,

    guy038

     
    Last edit: THEVENOT Guy 2013-08-04
  • csian
    csian
    2013-08-05

    thanks for your quick answers guys, looking forward for the solutions! :)

    @udesign: I know how to get to the search window, the question is what do you search for to get the aforementioned results

     
    Last edit: csian 2013-08-05
  • THEVENOT Guy
    THEVENOT Guy
    2013-08-05

    Hi csian,

    I'm back to you and, as promised, I give you my solution.

    But, first of all, don't forget that a script language could, also, do the job easily. For example, I don't doubt that Loreia2, a Python specialist :), could give you a solution, with a small python program !!

    So,

    • In a new tab, type the 24 lines, below, used as a test file.

    Test
    1111111111
    2222222222
    3333333333
    4444444444
    5555555555
    This is the Test to do
    First
    Second
    Third
    Fourth
    Fifth
    Test to do
    N° 1
    N° 2
    N° 3
    N° 4
    N° 5
    The last Test
    one
    two
    three
    four
    five


    So, the keyword will be the word Test, in this exact case

    • Open the search dialog, with the CTRL + F shortcut.

    • Check the regular expression search mode.

    • Uncheck the . matches newline box, if necessary.

    • Type .*Test(.*\R){3}\K.*\R in the Find what zone.

    • Click on the Find Next button => the line 3333333333 is selected.

    • Then, the three following clicks, on the Find Next button, selects, successively, the 3 lines Third, N° 3 and three.

    Just note that if you replace the number 3 with, let say, the number 5, in the regular search expression, then, this time, the search selects, instead, the 4 lines 5555555555, Fifth, N° 5 and five !

    Moreover, it also works if middle lines are empty lines.

    If I misunderstood your exact needs, don't hesitate to post some more explanations or, even, an example debug log file !

    Cheers,

    guy038

    EXPLANATION of the regular expression : .*Test(.*\R){3}\K.*\R

    • The part .*Test searches any text, even nothing, between a beginning of a line and the last occurrence of the word Test, in this line.

    • The part (.*\R){n} ( n represents a number ) searches all the text after the word Text, up to the end of the n-1 th complete line, with theirs end of line character(s).

    • The symbol \K means that the present search is, from now on, forgotten.

    • Finally, the search is, only, the part .*\R, which selects the entire line n, after the line, containing the keyword Test !


    You will find good documentation, about the new Perl Common Regular Expressions (PCRE), used by N++, since the 6.0 version, at the TWO addresses below :

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html

    http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

    • The FIRST link concerns the syntax of regular expressions in SEARCH

    • The SECOND link concerns the syntax of regular expressions in REPLACEMENT

     
    Last edit: THEVENOT Guy 2013-08-07
  • csian
    csian
    2013-08-13

    Thank you THEVENOT, that was great, exactly what I was looking for (needed a bit of time for testing).

    One more question, if I may: where is it defined that it should not only look for "3" but for "... selects, successively, the 3 lines Third, N° 3 and three."?

     
    Last edit: csian 2013-08-13
  • THEVENOT Guy
    THEVENOT Guy
    2013-08-14

    Hi Csian,

    Sorry, but I don't think that I correctly understood your question ! Anyway, I'll try to give you some more infos.

    In the general regular expression search : .*Test(.*\R){n}\K.*\R, you need to replace the variable n with a real number.

    So, if you replace n with the digit 2, for example, then, each time you click on the Find Next button, system goes ahead, searching for the word Test, in a line and, as soon as such a line is found, it selects the second line down this line.

    Of course, you can select all the second lines down, in only one operation, if you use the Mark tab of the search dialog

    So, with the same test text, of my precedent post :

    • Open the search dialog, with the CTRL + F shortcut

    • Click on the Mark tab

    • Check the Purge for each search box

    • Check the Bookmark line box, if, for example, you intend to copy all these bookmarked lines elsewhere,

    • Check the regular expression search mode.

    • Uncheck the . matches newline box, if necessary.

    • Type .*Test(.*\R){2}\K.*\R in the Find what zone.

    • Click on the Mark All button => the lines 2222222222, Second, N° 2 and two are automatically selected.

    Moreover, if these lines are bookmarked, you can copy/cut all these lines ( with the options Search\Bookmark\Copy Bookmarked lines or Search\Bookmark\Cut Bookmarked lines )

    Then, you just have to paste all these lines, elsewhere, with a simple CTRL + V

    Cheers,

    guy038

     
    Last edit: THEVENOT Guy 2013-08-14