Can your search term occur more than once in the file? If so, what do you want to happen when a second instance is fewer than ten lines after the first? Is it possible that a "Findme" occurs on the last line of the file and if so, what do you want to happen then? For example:

This is line1 At line 2. Did you Findme This is line3 This is line4 this is another Findme embedded in line 5 This is line 6 this is line 7 this is line 8 Line 9: yet another findme line 10 line 11 line 12 line 13 line 14 line 15 line 16 line 17 line 18 line 19 is the 10th line after the next-to-last instance of searchterm line 20 line 21 and so on... line 23 24: last Findme

IMO, you'll need no more than beginner skills to write a script (solution OR attempt-at-a-solution STILL left to the OP) that produces this output:

At line 2. Did you Findme This is line3 This is line4 this is another Findme embedded in line 5 This is line 6 this is line 7 this is line 8 Line 9: yet another findme line 10 line 11 line 12 line 13 line 14 line 15 line 16 line 17 line 18 line 19 is the 10th line after the next-to-last instance of searchterm 24: last Findme done

...but whether or not this is comensurate with your "requirement" (job? homework? other?) depends on the answers to the questions posed above.


In reply to Re: Searching for a string in a text and printing the next few lines by ww
in thread Searching for a string in a text and printing the next few lines by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.